• 2334阅读
  • 1回复

不用继承所有窗体实现移动 [复制链接]

上一主题 下一主题
离线sulwan
 

只看楼主 倒序阅读 楼主  发表于: 2019-11-19
— 本帖被 20091001753 从 Qt基础编程 移动到本区(2019-11-29) —
代码不是我原创的,但是我修正了过去的警告,还有一个就是我注解了源码,主要是最近闲来无事,跟着 刘大师学习,争取早日成为一名合格的粉丝
   文件
  1. #ifndef APPINIT_H
  2. #define APPINIT_H
  3. #include <QApplication>
  4. #include <QMouseEvent>
  5. #include <QMoveEvent>
  6. #include <QMutex>
  7. #include <QObject>
  8. #include <QPoint>
  9. #include <QWidget>
  10. class AppInit : public QObject {
  11.   Q_OBJECT
  12. public:
  13.   explicit AppInit(QObject *parent = nullptr);
  14.   static AppInit *getInstance();
  15.   void start();
  16. public:
  17.   static AppInit *instance;
  18. public slots:
  19.   bool eventFilter(QObject *obj, QEvent *event);
  20. private:
  21.   static AppInit *self;
  22. };
  23. #endif  // APPINIT_H

   文件
  1. #include "appinit.h"
  2. #include <QDebug>
  3. // 初始化静态变量
  4. AppInit *AppInit::self = nullptr;
  5. // 构造函数
  6. AppInit::AppInit(QObject *parent) : QObject(parent) {}
  7. // 启动函数
  8. void AppInit::start() {
  9.   // 监听过滤器安装到全局
  10.   qApp->installEventFilter(this);
  11. }
  12. // 获取实例
  13. AppInit *AppInit::getInstance() {
  14.   // 初始化类静态
  15.   static QMutex mutex;
  16.   if (!self) {
  17.     QMutexLocker locker(&mutex);
  18.     if (!self) {
  19.       self = new AppInit;
  20.     }
  21.   }
  22.   return self;
  23. }
  24. // 监听过滤器
  25. bool AppInit::eventFilter(QObject *obj, QEvent *event) {
  26.   // 把基类转换为界面类
  27.   QWidget *w = static_cast<QWidget *>(obj);
  28.   // 读取属性是否为真,为真是可以移动
  29.   if (!w->property("canMove").toBool()) {
  30.     // 把事件传递回基类
  31.     return QObject::eventFilter(obj, event);
  32.   }
  33.   // 鼠标是否按下
  34.   static bool mousePressed;
  35.   // 点位置
  36.   static QPoint mousePoint;
  37.   // 把事件强制转换为鼠标事件
  38.   QMouseEvent *evt = static_cast<QMouseEvent *>(event);
  39.   // 判断事件类型是不是鼠标左键
  40.   if (evt->type() == QEvent::MouseButtonPress) {
  41.     // 判断是否按下的左键
  42.     if (evt->button() == Qt::LeftButton) {
  43.       // 鼠标按下
  44.       mousePressed = true;
  45.       // 相对于桌面左上角原点坐标 - 距窗口左上上角去除边框的坐标
  46.       mousePoint = evt->globalPos() - w->pos();
  47.       return true;
  48.     }
  49.   } else if (event->type() == QEvent::MouseButtonRelease) {
  50.     // 鼠标抬起
  51.     mousePressed = false;
  52.     return true;
  53.   } else if (event->type() == QEvent::MouseMove) {
  54.     // 鼠标移动事件
  55.     // 鼠标左键按下,并没有抬起
  56.     qDebug() << mousePressed;
  57.     if (mousePressed && (evt->buttons() == Qt::LeftButton)) {
  58.       // 移动窗体位置
  59.       w->move(evt->globalPos() - mousePoint);
  60.       return true;
  61.     }
  62.   }
  63.   return QObject::eventFilter(obj, event);
  64. }
   使用
   1、在主函数main中调用
    
  1. AppInit::getInstance()->start();

   2、在需要移动的窗体构造函数中设置属性
  
  1. this->setProperty("canMove", true);

   代码到这里也就说完了,我继续烦着刘大师去,一个苦逼没事干的JAVA程序员


1条评分金钱+1
wreej123 金钱 +1 学到了 2020-02-20
离线big_mouse

只看该作者 1楼 发表于: 2020-03-25
快速回复
限100 字节
 
上一个 下一个