qt自定义含有拖动功能的窗口在点击窗口的下拉列表时窗口移动
生活随笔
收集整理的這篇文章主要介紹了
qt自定义含有拖动功能的窗口在点击窗口的下拉列表时窗口移动
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
提要
自定義的彈出窗口,窗口可以實現按下鼠標拖動,鼠標釋放停止拖動,窗口種含有子控件,下拉列表,在點擊下拉列表時窗口移動。
解決方法
因為點擊下拉列表的時候,觸發了窗口的移動事件,所以添加下拉列表的事件過濾。
下面附上實現代碼:
ui文件的結構如下:
在構造函數種安裝控件的事件過濾器。然后重寫過濾事件。
完整的代碼如下:
resolutiondialog.h
resolutiondialog.cpp
#include "resolutiondialog.h" #include "ui_resolutiondialog.h" #include <QStyledItemDelegate>ResolutionDialog::ResolutionDialog(QWidget *parent) :QDialog(parent),ui(new Ui::ResolutionDialog) {ui->setupUi(this);initResolutions();QStyledItemDelegate *delegate = new QStyledItemDelegate();ui->comboBoxReso->setItemDelegate(delegate);ui->comboBoxReso->installEventFilter(this);setWindowFlag(Qt::FramelessWindowHint);setAttribute(Qt::WA_TranslucentBackground); }ResolutionDialog::~ResolutionDialog() {delete ui; }void ResolutionDialog::initResolutions() {QList<QString> strList;strList.append("3840x2160");strList.append("1920x1080");strList.append("1680x1050");strList.append("1600x900");strList.append("1440x900");strList.append("1366x768");strList.append("1280x1024");QStringList strResoList(strList);ui->comboBoxReso->addItems(strResoList); }bool ResolutionDialog::eventFilter(QObject *obj, QEvent *event) {if(obj == ui->comboBoxReso){if(event->type() == QEvent::MouseMove){return true;}}return QDialog::eventFilter(obj,event); }void ResolutionDialog::mousePressEvent(QMouseEvent *event) {if (event->button() == Qt::LeftButton) {QPoint startPos = event->globalPos();m_offPos = startPos - geometry().topLeft();}QDialog::mousePressEvent(event); }void ResolutionDialog::mouseMoveEvent(QMouseEvent *event) {if (event->buttons() == Qt::LeftButton) {QPoint endPos = event->globalPos();move(endPos - m_offPos);}QDialog::mouseMoveEvent(event); }void ResolutionDialog::mouseReleaseEvent(QMouseEvent *event) {QDialog::mouseReleaseEvent(event); }void ResolutionDialog::onSetRowColSlot(int row, int col) {ui->spinBoxRow->setRange(0,row-1);ui->spinBoxCol->setRange(0,col-1); }void ResolutionDialog::on_closeBtn_clicked() {close(); }void ResolutionDialog::on_confirmBtn_clicked() {stuReso tempReso;tempReso.row = ui->spinBoxRow->value();tempReso.col = ui->spinBoxCol->value();QString strTemp = ui->comboBoxReso->currentText();QStringList strList = strTemp.split('x');QString strW = strList.first();QString strH = strList.last();tempReso.width = strW.toInt();tempReso.height = strH.toInt();emit sigEveryResolution(tempReso);accept(); }void ResolutionDialog::on_cancelBtn_clicked() {reject(); }上面只將這個出現上述問題的類的代碼附上。因為其中涉及到項目中的一些需求實現,讀者可以選擇性讀取,理解我說明的問題解決思路便好,代碼可以參考。由于涉及到qss文件設置樣式,那部分沒有貼出來,讀者可以注釋掉背景透明和無邊框的設置。
總結
以上是生活随笔為你收集整理的qt自定义含有拖动功能的窗口在点击窗口的下拉列表时窗口移动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器状态down,HAProxy的状态
- 下一篇: mysql event 变量_mysql