Programmatic QGraphicsView scrolling not updating properly

后端 未结 3 568
北海茫月
北海茫月 2021-01-12 19:20

I have a custom class derived from QGraphicsView that implements a slot call scrollHorizontal(int dx), inside the code is simply

void CustomView::scrollHoriz         


        
3条回答
  •  无人共我
    2021-01-12 19:39

    I've worked with QGraphicsView in Qt 4.6.3 - 4.7.2 and have to argue that you can use the respective QScrollBar in the following way:

    //graphics view initialization
    QGraphicsView *graphicsView = new QGraphicsView(parent);
    QGraphicsScene *scene = new QGraphicsScene(0,0,widthOfScene,heightOfScene,parent);
    graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView->setScene(scene);
    
    //in another method
    QScrollBar* yPos=graphicsView->verticalScrollBar();
    yPos->setValue((int) newValue);
    

    It does not matter if they are hidden or not. They will still respond to setValue(int) as long as you have a graphics scene that is larger than the graphics view.

    The QGraphicsView will also respond to ensureVisible, which moves the scrollbars to the appropriate location.

提交回复
热议问题