qt中拖动窗口widget
生活随笔
收集整理的這篇文章主要介紹了
qt中拖动窗口widget
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
提要
繼承與QDialog的窗口,窗口原本按住標題欄可以拖動窗口,但是設置了窗口的隱藏標題欄屬性后,窗口不再能夠拖動。或者繼承于QWidget的窗體,不具有窗口拖動功能。
本文實現繼承于窗口widget或者繼承于QDialog隱藏窗口標題欄的窗口鼠標按下后不放可以拖動,鼠標釋放后停止拖動。要想實現這樣的功能繼承于QWidget的窗體需要重寫鼠標按下,移動,釋放事件。繼承于QDialog窗口但是隱藏窗體標題欄的也是一樣的,需重寫上面的三個方法。
示例
namedialog.h
#ifndef NAMEDIALOG_H #define NAMEDIALOG_H#include <QDialog>/******類功能描述:設置大屏列表的名稱*****/ namespace Ui { class NameDialog; }class NameDialog : public QDialog {Q_OBJECTpublic:explicit NameDialog(QWidget *parent = nullptr);~NameDialog(); protected:void mousePressEvent(QMouseEvent *event);//鼠標點擊void mouseMoveEvent(QMouseEvent *event);//鼠標移動事件void mouseReleaseEvent(QMouseEvent *event);//鼠標釋放事件 private:Ui::NameDialog *ui;QPoint m_offPos;//鼠標點擊點與窗口左上角的距離 };#endif // NAMEDIALOG_Hnamedialog.cpp
#include "namedialog.h" #include "ui_namedialog.h" #include <QMouseEvent>NameDialog::NameDialog(QWidget *parent) :QDialog(parent),ui(new Ui::NameDialog) {ui->setupUi(this);setWindowFlag(Qt::FramelessWindowHint); }NameDialog::~NameDialog() {delete ui; }void NameDialog::mousePressEvent(QMouseEvent *event) {if (event->button() == Qt::LeftButton) {QPoint startPos = event->globalPos();m_offPos = startPos - geometry().topLeft();}QDialog::mousePressEvent(event); }void NameDialog::mouseMoveEvent(QMouseEvent *event) {if (event->buttons() == Qt::LeftButton) {QPoint endPos = event->globalPos();move(endPos - m_offPos);}QDialog::mouseMoveEvent(event); }void NameDialog::mouseReleaseEvent(QMouseEvent *event) {QDialog::mouseReleaseEvent(event); }以上重寫鼠標按下,移動和釋放事件便可以實現窗體的按下拖動。如果是繼承于QWidget窗體,則基類為QWidget,相應的在繼承于QWidget的類中實現鼠標的點擊,移動和釋放事件,函數的實現幾乎一樣,唯一不一樣的是,調用父類的點擊,移動和釋放事件,將QDialog::mousePressEvent(event);改為QWidget::mousePressEvent(event);依次類推,如下。
#ifndef NAMEDIALOG_H #define NAMEDIALOG_H#include <QDialog>/******類功能描述:設置大屏列表的名稱*****/ namespace Ui { class NameWidget; }class NameWidget: public QWidget {Q_OBJECTpublic:explicit NameWidget(QWidget *parent = nullptr);~NameWidget(); protected:void mousePressEvent(QMouseEvent *event);//鼠標點擊void mouseMoveEvent(QMouseEvent *event);//鼠標移動事件void mouseReleaseEvent(QMouseEvent *event);//鼠標釋放事件 private:Ui::NameWidget*ui;QPoint m_offPos;//鼠標點擊點與窗口左上角的距離 };#endif // NAMEDIALOG_H #include "namewidget.h" #include "ui_namewidget.h" #include <QMouseEvent>NameWidget::NameWidget(QWidget *parent) :QWidget(parent),ui(new Ui::NameWidget) {ui->setupUi(this);setWindowFlag(Qt::FramelessWindowHint); }NameWidget::~NameWidget() {delete ui; } void NameWidget::mousePressEvent(QMouseEvent *event) {if (event->button() == Qt::LeftButton) {QPoint startPos = event->globalPos();m_offPos = startPos - geometry().topLeft();}QWidget::mousePressEvent(event); }void NameWidget::mouseMoveEvent(QMouseEvent *event) {if (event->buttons() == Qt::LeftButton) {QPoint endPos = event->globalPos();move(endPos - m_offPos);}QWidget::mouseMoveEvent(event); }void NameWidget::mouseReleaseEvent(QMouseEvent *event) {QWidget::mouseReleaseEvent(event); }總結
以上是生活随笔為你收集整理的qt中拖动窗口widget的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql如何实现管理权限分离_基于Sp
- 下一篇: mysql+json+检索_如何从mys