Qt自由拖动无边框窗体(eventFilter事件过滤实现)

效果如下:

做项目中为了美观,经常需要对窗口标题栏,按钮进行自定义,因此常使用隐藏系统默认窗口边框选项 Qt::FramelessWindowHint,为使窗口能被随意拖动,使用Qt中的事件过滤器,重载eventFilter(QObject *obj, QEvent *evt)函数,每次拖拽事件发生时,返回当前事件触发窗体QObject进入事件过滤事件中,并对该窗体位置进行改变。

拖拽事件过滤在主函数中进行加载。每个需要支持拖拽的窗口设置自定义属性:this->setProperty("canMove",true);当属性canMove为真时支持拖拽。即在Main函数中加载过滤后不需要在窗体代码中引用拖拽模块。

窗体可拖拽功能模块代码:

appmove.h

#ifndef APPMOVE_H
#define APPMOVE_H

#include <QObject>
#include <QMutex>
#include <QEvent>
#include <QWidget>
#include <QApplication>
#include <QMouseEvent>

class AppMove:public QObject
{
public:
public:
    explicit AppMove(QObject *parent = 0);

    void start();

protected:
    bool eventFilter(QObject *obj, QEvent *evt);

};

#endif // APPMOVE_H

appmove.cpp

#include "appmove.h"

AppMove::AppMove(QObject *parent) : QObject(parent)
{

}

bool AppMove::eventFilter(QObject *obj, QEvent *evt)
{
    QWidget *w = (QWidget *)obj;
    if (!w->property("canMove").toBool()) {
        return QObject::eventFilter(obj, evt);
    }

    static QPoint mousePoint;
    static bool mousePressed = false;

    QMouseEvent *event = static_cast<QMouseEvent *>(evt);
    if (event->type() == QEvent::MouseButtonPress) {
        if (event->button() == Qt::LeftButton) {
            mousePressed = true;
            mousePoint = event->globalPos() - w->pos();
            return true;
        }
    } else if (event->type() == QEvent::MouseButtonRelease) {
        mousePressed = false;
        return true;
    } else if (event->type() == QEvent::MouseMove) {
        if (mousePressed && (event->buttons() && Qt::LeftButton)) {
            w->move(event->globalPos() - mousePoint);
            return true;
        }
    }

    return QObject::eventFilter(obj, evt);
}

void AppMove::start()
{
    qApp->installEventFilter(this);
}

主函数main:

#include "widget.h"
#include "appmove.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    AppMove move;
    move.start();

    Widget w;
    w.show();

    return a.exec();
}

DEMO下载地址:

https://download.csdn.net/download/zjgo007/12277364

发布了13 篇原创文章 · 获赞 23 · 访问量 4263

猜你喜欢

转载自blog.csdn.net/zjgo007/article/details/105159048