Qt工作笔记-QGraphicsProxyWidget放自定义界面实现拖动
生活随笔
收集整理的這篇文章主要介紹了
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_Hmycustomproxy.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_Hwidget.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_Hform.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_Hform.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)題。
- 上一篇: STL学习笔记-set的基本原理以及插入
- 下一篇: Qt|C++工作笔记-对虚函数的进一步认