Qt 鼠标左键推拽界面

代码实现:
button 返回 哪个按钮造成了此事件,buttons 返回 发生此事件时哪些按钮还处于按下状态

对于ComboBox这种控件有bug,我也不知道咋修改

cpp 复制代码
private:
	    // 记录坐标差值
    QPoint diff;
    QPoint now_pos;
    bool m_MouseDrag = false; // 一定要记得初始化为false

void Dialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
    	m_MouseDrag = true;
                      // 鼠标在全局的位置     // 界面左上角在全局的位置
        this->diff = event->globalPos() - this->frameGeometry().topLeft();
        // 如果是通过界面里的某个部件,进行拖拽移动,获取界面左上角在全局的位置是比较麻烦的
        // 需要用其父类来获取,直接调用parent()函数得到是Object类型,没有frameGeometry成员函数
        // 需要强转为父类 ((QWidget *)this->parent())->frameGeometry.topLeft()
        // 这里只是以父类为 QWidget 为例
        return;
    }
    QDialog::mousePressEvent(event);
}

void Dialog::mouseMoveEvent(QMouseEvent *event)
{
    if(m_MouseDrag && event->buttons() == Qt::LeftButton) {
        QPoint now_pos = event->globalPos() - this->diff;
        this->move(now_pos);
        return;
    }
    QDialog::mouseMoveEvent(event);
}

void Dialog::mouseReleaseEvent(QMouseEvent *event)
{
    // 释放鼠标事件(左键)
    if(event->button() == Qt::LeftButton){
        m_MouseDrag = false;
        return;
    }
    QDialog::mouseReleaseEvent(event);
}
相关推荐
xcyxiner4 小时前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 小时前
DicomViewer (添加模型类)3
qt
xcyxiner1 天前
DicomViewer (目录调整) 2
qt
xcyxiner1 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0063 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术3 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园3 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob3 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享3 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.3 天前
C语言--day30
c语言·开发语言