Results 1 to 5 of 5

Thread: QGraphicsView : scroll on drag

  1. #1
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QGraphicsView : scroll on drag

    Hi,

    I want to have my cake and eat it too :

    I want that when I drag on my GraphicsView I get a rubberband select box AND when I reach the edge of the scrollview while dragging the view scrolls to reveal the rest of the scene, so I can continue to include it under the rubberband box.

    How can I do that?

    Thanks
    -K

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView : scroll on drag

    Isn't it the default behaviour when using QGraphicsView::RubberBandDrag?

  3. #3
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsView : scroll on drag

    Hmm, not for me. I'm on Mac OS X 10.5 using qt 4.4.0

    -K

  4. #4
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsView : scroll on drag

    Bump. Any hint would be most appreciated -K

  5. #5
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView : scroll on drag

    Quote Originally Posted by kghose View Post
    Bump. Any hint would be most appreciated -K
    Well I had the same problem. And it turns out the graphicsview rubber band implementation is not particularly useful, so I made my own. The problem with the default is that it's relative to the view, so when you move the scene view, the selection rectangle moves with it, instead of anchoring to the scene where you clicked the mouse.

    So here's what I did:
    First of all, setDragMode(QGraphicsView::NoDrag); to turn off the default.

    Then in the view:
    Qt Code:
    1. void GraphWidget::onScrollTimeout() {
    2. if(scrollDirection & Left) {
    3. horizontalScrollBar()->setValue(horizontalScrollBar()->value()-scrollMagnitude);
    4. }
    5. if(scrollDirection & Right) {
    6. horizontalScrollBar()->setValue(horizontalScrollBar()->value()+scrollMagnitude);
    7. }
    8. if(scrollDirection & Up) {
    9. verticalScrollBar()->setValue(verticalScrollBar()->value()-scrollMagnitude);
    10. }
    11. if(scrollDirection & Down) {
    12. verticalScrollBar()->setValue(verticalScrollBar()->value()+scrollMagnitude);
    13. }
    14. if(rubberBand->isVisible()) { // update the rubber band
    15. QPoint mouseDownView = mapFromScene(mouseDownPos);
    16. QPoint diff = (lastMouseViewPos-mouseDownView);
    17. rubberBand->setGeometry(qMin(lastMouseViewPos.x(), mouseDownView.x()), qMin(lastMouseViewPos.y(), mouseDownView.y()), qAbs(diff.x()), qAbs(diff.y()));
    18. }
    19. }
    20.  
    21. void GraphWidget::mousePressEvent(QMouseEvent* evt) {
    22. // when shift is pressed, we drag the scene
    23. if(evt->modifiers().testFlag(Qt::ShiftModifier)) {
    24. setDragMode(QGraphicsView::ScrollHandDrag);
    25. qDebug() << "setting dragging mode";
    26. QGraphicsView::mousePressEvent(evt);
    27. }
    28. else {
    29. qDebug() << "press";
    30. scrollTimer.start(30);
    31. QGraphicsView::mousePressEvent(evt);
    32. if(!scene()->itemAt(mapToScene(evt->pos())) && scene()->selectedItems().isEmpty()) {
    33. mouseDownPos = mapToScene(evt->pos()).toPoint();
    34. startRubberBand = true;
    35. rubberBand->setGeometry(QRect(evt->pos(), evt->pos()));
    36. }
    37. }
    38. }
    39.  
    40. void GraphWidget::mouseMoveEvent(QMouseEvent* evt) {
    41. if(!evt->modifiers().testFlag(Qt::ShiftModifier) && evt->buttons().testFlag(Qt::LeftButton)) {
    42. QPoint pos = evt->pos();
    43. int distance = 0;
    44. scrollDirection = scrollMagnitude = 0;
    45. // determine the direction of automatic scrolling
    46. if(pos.x() < SCROLL_DISTANCE) {
    47. scrollDirection = Left;
    48. distance = pos.x();
    49. }
    50. else if(width()-pos.x() < SCROLL_DISTANCE) {
    51. scrollDirection = Right;
    52. distance = width()-pos.x();
    53. }
    54. if(pos.y() < SCROLL_DISTANCE) {
    55. scrollDirection |= Up;
    56. distance = pos.y();
    57. }
    58. else if(height()-pos.y() < SCROLL_DISTANCE) {
    59. scrollDirection |= Down;
    60. distance = height()-pos.y();
    61. }
    62. if(scrollDirection) {
    63. scrollMagnitude = qRound((SCROLL_DISTANCE-distance)/8);
    64. }
    65.  
    66. // handle the rubberband
    67. if(startRubberBand && (mapFromScene(mouseDownPos)-evt->pos()).manhattanLength() > 4) {
    68. rubberBand->show();
    69. startRubberBand = false;
    70. }
    71. if(rubberBand->isVisible()) {
    72. QPoint mouseDownView = mapFromScene(mouseDownPos);
    73. lastMouseViewPos = evt->pos();
    74. QRect rubberRect(mouseDownView, lastMouseViewPos);
    75. rubberRect = rubberRect.normalized();
    76. rubberBand->setGeometry(rubberRect);
    77. projectScene->setSelectionAreaFixed(QRectF(mapToScene(rubberRect.topLeft()), mapToScene(rubberRect.bottomRight())));
    78. }
    79.  
    80. }
    81. QGraphicsView::mouseMoveEvent(evt);
    82. }
    83.  
    84. void GraphWidget::mouseReleaseEvent(QMouseEvent* evt) {
    85. rubberBand->hide();
    86. startRubberBand = false;
    87. scrollDirection = scrollMagnitude = 0;
    88. scrollTimer.stop();
    89. QGraphicsView::mouseReleaseEvent(evt);
    90. setDragMode(QGraphicsView::NoDrag);
    91. }
    To copy to clipboard, switch view to plain text mode 

    So when we press the mouse, we store the mouse down pos and unhide the QRubberBand. Then on mouse move we update the QRubberBand's geometry and select the underlying objects. (on Line 77, I call the function "setSelectionAreaFixed". You can use QGraphicsScene::setSelectionArea if you are using Qt >= 4.4. Versions before 4.4 had a bug in that function that made it not work properly)

    To get the automatic scrolling of the view, I just start a timer when the drag starts. On mouse movement, if the cursor is close to the edge of the view, I set a variable to indicate the direction of scrolling and the speed, then on the timeout I move the scene.

    It's not exactly elegant, but it works. Took me a while to sort everything out (the Qt bug didn't help)
    Last edited by pherthyl; 14th August 2008 at 22:00.

  6. The following user says thank you to pherthyl for this useful post:

    kghose (14th August 2008)

Similar Threads

  1. how to add scroll bar on QGridLayout in QT??
    By sharvari in forum Newbie
    Replies: 3
    Last Post: 19th February 2008, 14:31
  2. QScrollArea's Scroll Bars
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 19th September 2006, 13:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.