QGraphicsView: Disable automatic scrolling

Sebastian Mach picture Sebastian Mach · Jan 14, 2011 · Viewed 7.1k times · Source

I want to have a QGraphicsView that never scrolls automatically.

Similar: Basically, my question is identical to http://developer.qt.nokia.com/forums/viewthread/2220 , but that thread didn't receive an answer.

What I have tried so far:

  • Within showEvent() and resizeEvent(), I do ui->graphicsView->fitInView(...), which works fine as long as items do not overshoot the screen-rectangle
  • I've also tried manipulating the view transform, but apart from scaling it's coefficients never change, so this was fruitless, too
  • Diabling scroll bar appearance does not help, too

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

Answer

anonvt picture anonvt · Feb 25, 2011

My solution is a bit sketchy but I think it's pretty intuitive: If you don't want the QGraphicsView to ever scroll your stuff, override the virtual method scrollContentsBy.

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