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