Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What should I do if I want to drag QGraphicsVIew with the mouse middleButton?

What should I do if I want to drag QGraphicsVIew with the mouse middleButton?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 291 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Van
    wrote on last edited by
    #1

    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();
    }
    
    
    Pl45m4P 1 Reply Last reply
    0
    • J John Van

      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();
      }
      
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @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

      • https://gist.github.com/gboeer/a00760b6d7af32c01357fb7ff76ad86a

      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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • J John Van has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved