日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Qt工作笔记-QGraphicsProxyWidget放自定义界面实现拖动

發(fā)布時(shí)間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt工作笔记-QGraphicsProxyWidget放自定义界面实现拖动 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原理:

因?yàn)樵谝晥D中,所以和傳統(tǒng)的widget中界面拖動(dòng)不一樣!
要把坐標(biāo)轉(zhuǎn)化為視圖的坐標(biāo)才行!

運(yùn)行截圖如下:

里面的界面是這樣的:

?

程序結(jié)構(gòu)如下:

?

源碼如下:

form.h

#ifndef FORM_H #define FORM_H#include <QWidget>namespace Ui { class Form; }class Form : public QWidget {Q_OBJECTpublic:explicit Form(QWidget *parent = 0);~Form();private:Ui::Form *ui; };#endif // FORM_H

mycustomproxy.h

#ifndef MYCUSTOMPROXY_H #define MYCUSTOMPROXY_H#include <QGraphicsProxyWidget> #include <QPointF>class MyCustomProxy : public QGraphicsProxyWidget {Q_OBJECT public:MyCustomProxy(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);void initWindowsFrame();protected:void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;private:QPointF m_z; };#endif // MYCUSTOMPROXY_H

widget.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE class QGraphicsScene; class MyCustomProxy; QT_END_NAMESPACEclass Form;namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;QGraphicsScene *m_scene;MyCustomProxy *m_myCustomProxy;Form *m_form; };#endif // WIDGET_H

form.cpp'

#include "form.h" #include "ui_form.h"Form::Form(QWidget *parent) :QWidget(parent),ui(new Ui::Form) {ui->setupUi(this); }Form::~Form() {delete ui; }

main.cpp

#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }

mycustomproxy.cpp

#include "mycustomproxy.h" #include <QGraphicsSceneMouseEvent> #include <QWidget>MyCustomProxy::MyCustomProxy(QGraphicsItem *parent, Qt::WindowFlags wFlags): QGraphicsProxyWidget(parent, wFlags) {}void MyCustomProxy::initWindowsFrame() {setWindowFlags(Qt::Window | Qt::FramelessWindowHint); }void MyCustomProxy::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {QGraphicsProxyWidget::mouseMoveEvent(event);QPointF y = mapToScene(event->pos());QPointF x = y - m_z;if(widget() != NULL)setGeometry(QRectF(x.rx(), x.ry(), widget()->width(), widget()->height())); }void MyCustomProxy::mousePressEvent(QGraphicsSceneMouseEvent *event) {QGraphicsProxyWidget::mousePressEvent(event);QPointF y = mapToScene(event->pos());QPointF x = scenePos();m_z = y - x; }void MyCustomProxy::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {QGraphicsProxyWidget::mouseReleaseEvent(event);m_z = QPointF(); }

widget.cpp

#include "widget.h" #include "ui_widget.h" #include "mycustomproxy.h" #include "form.h"#include <QGraphicsScene>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);this->setWindowTitle("CSDN IT1995");ui->graphicsView->setRenderHints(ui->graphicsView->renderHints()| QPainter::Antialiasing| QPainter::SmoothPixmapTransform);m_scene = new QGraphicsScene;m_scene->setSceneRect(-500, -500, 500, 500);ui->graphicsView->setScene(m_scene);m_myCustomProxy = new MyCustomProxy;m_myCustomProxy->setWindowFlags(Qt::Window);m_myCustomProxy->initWindowsFrame();m_form = new Form;m_myCustomProxy->setWidget(m_form);m_myCustomProxy->setPos(-m_form->width(), -m_form->height());m_scene->addItem(m_myCustomProxy); }Widget::~Widget() {delete ui; }

?

但是,這樣做只能靠點(diǎn)擊控件,才能拖動(dòng),點(diǎn)界面其他地方就不能脫了。

只要這么做,就可以實(shí)現(xiàn)界面其他地方也可以拖動(dòng)!

重寫(xiě)Form中的mousePressEvent函數(shù)

如下:

form.h

#ifndef FORM_H #define FORM_H#include <QWidget>namespace Ui { class Form; }class Form : public QWidget {Q_OBJECTpublic:explicit Form(QWidget *parent = 0);~Form();protected:void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;private:Ui::Form *ui; };#endif // FORM_H

form.cpp

#include "form.h" #include "ui_form.h"Form::Form(QWidget *parent) :QWidget(parent),ui(new Ui::Form) {ui->setupUi(this); }Form::~Form() {delete ui; }void Form::mousePressEvent(QMouseEvent *event) {}

千萬(wàn)不要再調(diào)用他父類(lèi)的mousePressEvent!!!!

總結(jié)

以上是生活随笔為你收集整理的Qt工作笔记-QGraphicsProxyWidget放自定义界面实现拖动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。