简体   繁体   中英

Qt QGraphicsview how to hook to resize event

we're working on a Qt C++ Widget project and recently ve'we run into trouble. We're Qt rookies.

On our widget there are two QGraphicsView which need to resize automatically when the window is resized (we've done that) and keep the content inside them resized/fit/scaled accordingly to...

So we've figured we need to somehow hook to onResizeEvent or find which slot does it. But we're somehow lost as to how to do it.

PS: Please excuse my english.

If you have a QWidget you only need to reimplement the resizeEvent function.

void QWidget::resizeEvent ( QResizeEvent * event ) [virtual protected]

In the Qt documentation you can find the scribble example for a full code example: http://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html

Alternatively you can install an event handler and grab the QEvent::Resize . How to install event filters is described here: http://qt-project.org/doc/qt-4.8/eventsandfilters.html

You can make your custom class which inherits from QGraphicsView. You should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like:

void MyView::resizeEvent(QResizeEvent *event)
{

    fitInView(0, 0, 500, 500,Qt::KeepAspectRatio);

    QGraphicsView::resizeEvent(event);
}

This way the view will always display the whole scene. Ie if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.

If you are resizing in some slot then you can use sender() . Btw you can always use debugger or logs/backtraces to get such information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM