qt android 悬浮窗,Qt 自定义悬浮窗(类似QQ拼音输入法)
1.運行效果
實現功能:
1.可拖動。
2.可顯示,可隱藏?。
3.懸浮在主界面上面。
4.目前只是一個簡單的界面,什么功能都沒有。
2.ui界面
3.源碼
//FloatingWindow.h
#ifndef FLOATINGWINDOW_H
#define FLOATINGWINDOW_H
#include
#include
#include
namespace Ui {
class FloatingWindow;
}
class FloatingWindow : public QWidget
{
Q_OBJECT
public:
explicit FloatingWindow(QWidget *parent = 0);
~FloatingWindow();
private:
Ui::FloatingWindow *ui;
protected:
//事件過濾
bool eventFilter(QObject *watched, QEvent *event);
private:
QPropertyAnimation *m_pAnimation;
bool m_bDragFlag;
QPoint m_pointDrag;
};
#endif // FLOATINGWINDOW_H
//FloatingWindow.cpp
#include "FloatingWindow.h"
#include "ui_FloatingWindow.h"
#include
FloatingWindow::FloatingWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::FloatingWindow),
m_pAnimation(nullptr),
m_bDragFlag(false)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow | Qt::WindowStaysOnTopHint);
ui->btnQQ->installEventFilter(this);
ui->widget->installEventFilter(this);
ui->widget->hide();
setAutoFillBackground(false);
setAttribute(Qt::WA_TranslucentBackground);
ui->frame->setStyleSheet("background:transparent;");
}
FloatingWindow::~FloatingWindow()
{
delete ui;
}
bool FloatingWindow::eventFilter(QObject *target, QEvent *event)
{
QMouseEvent *mouse = dynamic_cast(event);
if (target == ui->btnQQ)
{
if (event->type() == QEvent::Enter)
{
ui->widget->show();
}
if (event->type() == QEvent::Leave)
{
ui->widget->hide();
}
}
//拖動
if (mouse)
{
if (mouse->button() == Qt::LeftButton && mouse->type() == QEvent::MouseButtonPress)
{
m_bDragFlag = true;
if (target == ui->btnQQ)
{
m_pointDrag = ui->frame->mapToParent(ui->btnQQ->mapToParent(mouse->pos()));
ui->widget->hide();
}
}
else if (mouse->type() == QEvent::MouseButtonRelease)
{
m_bDragFlag = false;
}
else if (m_bDragFlag && mouse->type() == QEvent::MouseMove)
{
this->move(mouse->globalPos() - m_pointDrag);
}
}
return QWidget::eventFilter(target, event);
}
4.完整代碼
總結
以上是生活随笔為你收集整理的qt android 悬浮窗,Qt 自定义悬浮窗(类似QQ拼音输入法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言C++情人节红玫瑰代码
- 下一篇: 旋转编码器详解(主要讨论增量式编码器与绝