What should I do if I want to drag QGraphicsVIew with the mouse middleButton?
-
Hello!
The default is to drag the view with the leftButton, I want to change it to the middleButton.
Another question is why adding the line "setSceneRect(-100, -100, 200, 200);"makes it impossible to drag the view.#include <QApplication> #include <QGraphicsSceneMouseEvent> class MyScene : public QGraphicsScene { public: QImage m_image; protected: void drawBackground(QPainter* painter, const QRectF& rect) { if (!m_image.isNull()) { auto r = sceneRect(); painter->drawImage(r, m_image); } } void mousePressEvent(QGraphicsSceneMouseEvent* e) { switch (e->buttons()) { case Qt::LeftButton: printf("do something\n"); break; case Qt::RightButton: break; case Qt::MiddleButton: QGraphicsScene::mousePressEvent(e); break; } } }; class MyView :public QGraphicsView { public: MyView() { auto m_scene = new MyScene; m_scene->m_image = QImage("D:/100.png"); m_scene->setBackgroundBrush(Qt::gray); m_scene->setSceneRect(0, 0, 400, 400); //setSceneRect(-100, -100, 200, 200); setScene(m_scene); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setOptimizationFlags(QGraphicsView::DontSavePainterState); setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setRenderHint(QPainter::Antialiasing, false); setDragMode(QGraphicsView::ScrollHandDrag); } void wheelEvent(QWheelEvent* event) { auto r = sceneRect(); double scaleFactor = pow(2.0, event->angleDelta().y() / 240.0); scale(scaleFactor, scaleFactor); }; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); auto v = new MyView(); v->resize(800, 600); v->show(); return a.exec(); } -
Hello!
The default is to drag the view with the leftButton, I want to change it to the middleButton.
Another question is why adding the line "setSceneRect(-100, -100, 200, 200);"makes it impossible to drag the view.#include <QApplication> #include <QGraphicsSceneMouseEvent> class MyScene : public QGraphicsScene { public: QImage m_image; protected: void drawBackground(QPainter* painter, const QRectF& rect) { if (!m_image.isNull()) { auto r = sceneRect(); painter->drawImage(r, m_image); } } void mousePressEvent(QGraphicsSceneMouseEvent* e) { switch (e->buttons()) { case Qt::LeftButton: printf("do something\n"); break; case Qt::RightButton: break; case Qt::MiddleButton: QGraphicsScene::mousePressEvent(e); break; } } }; class MyView :public QGraphicsView { public: MyView() { auto m_scene = new MyScene; m_scene->m_image = QImage("D:/100.png"); m_scene->setBackgroundBrush(Qt::gray); m_scene->setSceneRect(0, 0, 400, 400); //setSceneRect(-100, -100, 200, 200); setScene(m_scene); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setOptimizationFlags(QGraphicsView::DontSavePainterState); setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setRenderHint(QPainter::Antialiasing, false); setDragMode(QGraphicsView::ScrollHandDrag); } void wheelEvent(QWheelEvent* event) { auto r = sceneRect(); double scaleFactor = pow(2.0, event->angleDelta().y() / 240.0); scale(scaleFactor, scaleFactor); }; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); auto v = new MyView(); v->resize(800, 600); v->show(); return a.exec(); }@John-Van said in What should I do if I want to drag QGraphicsVIew with the mouse middleButton?:
I want to change it to the middleButton.
Quick google search
Another question is why adding the line "setSceneRect(-100, -100, 200, 200);"makes it impossible to drag the view.
Because you set a static scene rect. There is nothing to move. The scene is fixed 200x200 to that point...
If you add items and the scene will grow, you can move again. -
J John Van has marked this topic as solved on