QGraphicsView: disable automatic scrolling

I want to have a QGraphicsView that never scrolls automatically.

It seems: in principle, my question is identical to http://developer.qt.nokia.com/forums/viewthread/2220 , but this thread did not receive an answer.

What I have tried so far:

  • Inside showEvent () and resizeEvent (), I do ui-> graphicsView-> fitInView (...), which works fine until the elements skip the screen rectangle
  • I also tried to manipulate the view transformation, but in addition to scaling, the coefficients never change, so it was also fruitless.
  • The appearance of the scrollbar does not help either

See also http://doc.qt.io/qt-4.8/qgraphicsview.html .

+6
qt scroll qgraphicsview
source share
5 answers

I found a solution (feel free to post my alternatives :)), yet I thought this answer might be useful, as I struggled to work with Google and the documentation for 15 hours.

The key should not only call fitInView (), but also setSceneRect (). This did it for me (replace FooBar with your own class name):

void FooBar::resizeEvent(QResizeEvent *) { fitView(); } void FooBar::showEvent(QShowEvent *) { fitView(); } void FooBar::fitView() { const QRectF rect = QRectF(-0.5,-0.5, 1, 1); ui->graphicsView->fitInView(rect, Qt::KeepAspectRatio); ui->graphicsView->setSceneRect(rect); } 
+4
source share

My solution is a bit sketchy, but I think it is pretty intuitive. If you do not want QGraphicsView to ever scroll through your stuff, override the scrollContentsBy virtual method.

 void QGraphicsViewDerived::scrollContentsBy(int, int) { //don't do anything hah! } 
+7
source share

view->setDragMode(QGraphicsView::NoDrag); did the trick for me.

Anonvt's solution also worked, except that it interfered with the lighting of the scene.

+1
source share

I found the perfect solution by calling

 QGraphicsView::setSceneRect(yourScene->sceneRect()) 

in the constructor of your view, the automatic scroll behavior stops.

+1
source share

Another simple solution, if interactivity is not required, is to disable QGraphicsView:

 view->setEnabled(false); 

This will also prevent it from scrolling, however it will also not accept mouse or keyboard events.

0
source share

All Articles