Results 1 to 9 of 9

Thread: QGraphicsView scrolling problem with 4.3.0

  1. #1
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView scrolling problem with 4.3.0

    To work around the bug described in this post, I installed Qt 4.3.0 now. The bug seems fixed indeed, however, I am now experiencing another problem that was not present with Qt 4.2.3.

    I derived a class from QGraphicsView, and did a custom implementation for scrolling when the user clicks on the background. That implementation worked flawlessly with Qt 4.2.3, but in Qt 4.3.0, it only works if I set at least one of the scrollbar policies not to ...AlwaysOff.

    I attached a minimal self-containing compilable example below. I'd have said it's a bug introduced in 4.3.0, but in contrast to the original code, that example doesn't work as expected even if compiled against 4.2.3...

    I am puzzled. Any help would be greatly appreciated.

    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsView>
    3. #include <QGraphicsTextItem>
    4. #include <QGraphicsScene>
    5. #include <QMouseEvent>
    6. #include <QScrollBar>
    7.  
    8. class MyWidget : public QGraphicsView
    9. {
    10. public:
    11. MyWidget(QGraphicsScene *scene, QWidget *parent = 0);
    12.  
    13. protected:
    14. virtual void mousePressEvent(QMouseEvent *event);
    15. virtual void mouseReleaseEvent(QMouseEvent *event);
    16. virtual void mouseMoveEvent(QMouseEvent *event);
    17.  
    18. private:
    19. void storeMouseEvent(QMouseEvent *event);
    20. bool scrolling;
    21. QMouseEvent lastMouseEvent;
    22. };
    23.  
    24. MyWidget::MyWidget(QGraphicsScene *scene, QWidget *parent)
    25. : QGraphicsView(scene, parent), scrolling(false),
    26. lastMouseEvent(QEvent::None,QPoint(),Qt::NoButton,Qt::NoButton,Qt::NoModifier)
    27. {
    28. // If at least one of these has the policy ..AlwaysOn, scrolling works
    29. // (in both directions)!
    30. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    31. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    32. }
    33.  
    34. void MyWidget::storeMouseEvent(QMouseEvent *event)
    35. {
    36. lastMouseEvent = QMouseEvent(event->type(), event->pos(),
    37. event->globalPos(),
    38. event->button(),event->buttons(),event->modifiers());
    39. }
    40.  
    41. void MyWidget::mousePressEvent(QMouseEvent *event)
    42. {
    43. // just handle left button presses on the background
    44. QGraphicsItem *item = itemAt(event->pos());
    45.  
    46. if(!item) {
    47. if((event->button() == Qt::LeftButton)) {
    48. scrolling = true;
    49. storeMouseEvent(event);
    50. event->accept();
    51. return;
    52. }
    53. }
    54. event->ignore();
    55. QGraphicsView::mousePressEvent(event);
    56. }
    57.  
    58. void MyWidget::mouseMoveEvent(QMouseEvent *event)
    59. {
    60. // perform scrolling if scrolling is active
    61. if(scrolling) {
    62. QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
    63. QScrollBar *hBar = horizontalScrollBar();
    64. QScrollBar *vBar = verticalScrollBar();
    65. hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
    66. vBar->setValue(vBar->value() - delta.y());
    67. storeMouseEvent(event);
    68. event->accept();
    69. return;
    70. }
    71. event->ignore();
    72. QGraphicsView::mouseMoveEvent(event);
    73. }
    74.  
    75. void MyWidget::mouseReleaseEvent(QMouseEvent *event)
    76. {
    77. // end scrolling mode if it was active
    78. if((event->button() == Qt::LeftButton) && (scrolling == true)) {
    79. scrolling = false;
    80. storeMouseEvent(event);
    81. event->accept();
    82. return;
    83. }
    84. event->ignore();
    85. QGraphicsView::mouseReleaseEvent(event);
    86. }
    87.  
    88. int main(int argc, char *argv[])
    89. {
    90. QApplication app(argc, argv);
    91. MyWidget widget(&scene);
    92. QGraphicsTextItem item("hello");
    93. scene.addItem(&item);
    94. widget.show();
    95. scene.setSceneRect(-1000,-1000,2000,2000);
    96. return app.exec();
    97. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Hi,
    I reported this problem to the Qt Task tracker some time ago. It got the issue nr #167439 but I can't seem to find it anymore. Anyway the trolls answered:

    Thank you for your bug-report.

    > QGraphicsView ScrollBarPolicy behaviour changed with Qt 4.3
    >
    > What I did:
    > Created a QGraphicsView and I set both scrollbarPolicies to
    > ScrollBarAlwaysOff and the DragMode to ScrollHandDrag.
    >
    > What I expected to see:
    > Up until Qt 4.2 I could use the hand or mouse wheel to scroll.

    Happy days anyway. This bug is fixed in the latest snapshot, and will be
    a part of the 4.3.1 release.

  3. The following user says thank you to spud for this useful post:

    hb (28th June 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Try QAbstractScrollArea::scrollContentsBy():
    Qt Code:
    1. scrollContentsBy((isRightToLeft() ? -delta.x() : delta.x()), delta.y());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #4
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Thanks spud. I'll wait for 4.3.1 to come out then, hoping that it will indeed fix this problem.

  6. #5
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Quote Originally Posted by jpn View Post
    I was thinking about that function as well, but this quote from the docs made me not use it:

    Calling this function in order to scroll programmatically is an error, use the scroll bars instead (e.g. by calling QScrollBar::setValue() directly).

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Quote Originally Posted by hb View Post
    I was thinking about that function as well, but this quote from the docs made me not use it:
    Good notice. Anyway, I'm not exactly sure if it really causes any problems in this case since scroll bar policies are set to Qt::ScrollBarAlwaysOff. One would have to explore the sources a bit to find out..
    J-P Nurmi

  8. #7
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Quote Originally Posted by jpn View Post
    Good notice. Anyway, I'm not exactly sure if it really causes any problems in this case since scroll bar policies are set to Qt::ScrollBarAlwaysOff. One would have to explore the sources a bit to find out..
    True, but I'd rather not go for that. Even if it works in this version, you can never know about future versions if that use-case is so directly discouraged in the docs. Thanks anyways

  9. #8
    Join Date
    Aug 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Quote Originally Posted by hb View Post
    Thanks spud. I'll wait for 4.3.1 to come out then, hoping that it will indeed fix this problem.
    Is it just me, or is this broken in 4.3.1 too?

  10. #9
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scrolling problem with 4.3.0

    Hm... the regression with scrollbars was fixed in 4.3.1, and there were no outstanding QGV bugs at the time 4.3.1 was released.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

Similar Threads

  1. QTreeView problem scrolling to end.
    By seneca in forum Qt Programming
    Replies: 7
    Last Post: 22nd December 2015, 12:08
  2. Qt 4.3.0 clipping problem?
    By macbeth in forum Qt Programming
    Replies: 6
    Last Post: 13th June 2007, 17:56
  3. Replies: 3
    Last Post: 20th February 2007, 13:02
  4. Replies: 2
    Last Post: 8th October 2006, 20:14

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.