How to use the QGraphicsView's translate() function?

后端 未结 4 728
暖寄归人
暖寄归人 2021-01-11 14:07

here i have a scene and a associated view ,then i hava a position in the scene coordinates.I want to set the center of the viewport with the position.How can i do it? I try

4条回答
  •  花落未央
    2021-01-11 14:57

    Center

    As said Frank Osterfeld, to center your viewport at a given position, you can simply use the function centerOn.

    Translate

    But to translate your viewport, it exists another way that consists to change your scrollbars position :

    // Your graphics view
    QGraphicsView *view;
    
    // dx, dy corresponds to your translation
    int dx, dy;
    
    // Change scrollbars position
    view->horizontalScrollBar()->setValue( view->horizontalScrollBar()->value() + dx );
    view->verticalScrollBar()->setValue( view->verticalScrollBar()->value() + dy );
    

    Render

    If needed, you can also hide the scrollbars :

    // Hide the scrollbars
    view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    

提交回复
热议问题