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. How to move QGraphicsScene by dragging with mouse?
Forum Updated to NodeBB v4.3 + New Features

How to move QGraphicsScene by dragging with mouse?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 9.8k 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hey,
    I already found this thread: https://stackoverflow.com/questions/35865161/qt-graphic-scene-view-moving-around-with-mouse
    But this doesn't work in my program, the scene just goes back every time I move the mouse (pressed) to the point where I first clicked on the scene... (I don't really understynd that too). Maybe it has something to do with the fact, that my scene is larger than its viewport.
    Any ideas how I could make this?
    Thanks for answers

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Showing your implementation will likely helps us help you finding what's going wrong.

      You should also which version of Qt your are using and on which platform.

      NiagarerN Offline
      NiagarerN Offline
      Niagarer
      wrote on last edited by Niagarer
      #3

      @SGaist
      Yes thank you, I will keep it in mind.
      I got a solution just now:
      I use the scrollBars. When the mouse is dragging, I change the scrollbars depending on the QMouseEvent x and y.
      Here is the implementation:

      void GraphWidget::mousePressEvent(QMouseEvent *event){
          if (event->button() == Qt::RightButton)
          {
              rightMousePressed = true;
              _panStartX = event->x();
              _panStartY = event->y();
              setCursor(Qt::ClosedHandCursor);
              event->accept();
              return;
          }
      }
      
      void GraphWidget::mouseReleaseEvent(QMouseEvent *event){
          if (event->button() == Qt::RightButton)
          {
              rightMousePressed = false;
              setCursor(Qt::ArrowCursor);
              event->accept();
              return;
          }
          event->ignore();
      }
      
      void GraphWidget::mouseMoveEvent(QMouseEvent *event){
          if (rightMousePressed)
          {
              horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - _panStartX));
              verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - _panStartY));
              _panStartX = event->x();
              _panStartY = event->y();
              event->accept();
              return;
          }
          event->ignore();
      
      }
      

      This is a very easy way to do this and I think it should work for everyone.
      I got the answer here: https://stackoverflow.com/questions/4753681/how-to-pan-images-in-qgraphicsview

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Showing your implementation will likely helps us help you finding what's going wrong.

        You should also which version of Qt your are using and on which platform.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        NiagarerN 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Showing your implementation will likely helps us help you finding what's going wrong.

          You should also which version of Qt your are using and on which platform.

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by Niagarer
          #3

          @SGaist
          Yes thank you, I will keep it in mind.
          I got a solution just now:
          I use the scrollBars. When the mouse is dragging, I change the scrollbars depending on the QMouseEvent x and y.
          Here is the implementation:

          void GraphWidget::mousePressEvent(QMouseEvent *event){
              if (event->button() == Qt::RightButton)
              {
                  rightMousePressed = true;
                  _panStartX = event->x();
                  _panStartY = event->y();
                  setCursor(Qt::ClosedHandCursor);
                  event->accept();
                  return;
              }
          }
          
          void GraphWidget::mouseReleaseEvent(QMouseEvent *event){
              if (event->button() == Qt::RightButton)
              {
                  rightMousePressed = false;
                  setCursor(Qt::ArrowCursor);
                  event->accept();
                  return;
              }
              event->ignore();
          }
          
          void GraphWidget::mouseMoveEvent(QMouseEvent *event){
              if (rightMousePressed)
              {
                  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - _panStartX));
                  verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - _panStartY));
                  _panStartX = event->x();
                  _panStartY = event->y();
                  event->accept();
                  return;
              }
              event->ignore();
          
          }
          

          This is a very easy way to do this and I think it should work for everyone.
          I got the answer here: https://stackoverflow.com/questions/4753681/how-to-pan-images-in-qgraphicsview

          1 Reply Last reply
          2

          • Login

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