Qt无边框

最简单的可拖动对话框(大小不可改变)

cpp 复制代码
#ifndef DIALOG_H
#define DIALOG_H

/**
 * @file dialog.h
 * @author lpl
 * @brief 无边框dialog类
 * @date 2024/06/05
 */
#include <QDialog>
#include <QMouseEvent>
namespace Ui {
class Dialog;
}
/**
 * @brief The Dialog class
 * 无边框dialog
 */
class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    /**
     * @brief mousePressEvent   鼠标点击事件
     * @param ev 鼠标事件
     */
    virtual void mousePressEvent(QMouseEvent *ev) override;
    /**
     * @brief mouseMoveEvent   鼠标移动事件
     * @param ev 鼠标事件
     */
    virtual void mouseMoveEvent(QMouseEvent *ev) override;
    /**
     * @brief mouseReleaseEvent   鼠标释放事件
     * @param ev 鼠标事件
     */
    virtual void mouseReleaseEvent(QMouseEvent *ev) override;
private:
    Ui::Dialog *ui;
    bool                    m_bIsPress;        ///< 是否点击鼠标
    QPoint                  m_pointMoveDis;    ///< 鼠标与电脑窗体左上角的相对位置
};

#endif // DIALOG_H
cpp 复制代码
#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include <QMouseEvent>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::mousePressEvent(QMouseEvent *ev)
{
    qDebug()<<ev->globalPos();
    //按下鼠标左键
    if(Qt::LeftButton == ev->button()){
        m_pointMoveDis = ev->globalPos();
        m_bIsPress = true;
    }
    QDialog::mousePressEvent(ev);
}

void Dialog::mouseMoveEvent(QMouseEvent *ev)
{
     if (ev->buttons() & Qt::LeftButton)
     {
         if(m_bIsPress)
         {
             QPoint movePos = ev->globalPos();
             this->move(this->pos() + movePos - m_pointMoveDis);
             m_pointMoveDis = movePos;
         }
     }
     QDialog::mouseMoveEvent(ev);
}

void Dialog::mouseReleaseEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        m_bIsPress = false;
    }

    QDialog::mouseReleaseEvent(ev);
}
相关推荐
Quz5 小时前
QML Hello World 入门示例
qt
xcyxiner3 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner4 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner4 天前
DicomViewer (添加模型类)3
qt
xcyxiner5 天前
DicomViewer (目录调整) 2
qt
xcyxiner5 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0067 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术7 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园7 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob7 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio