简体   繁体   English

Qt QGraphicsview如何挂钩以调整大小事件

[英]Qt QGraphicsview how to hook to resize event

we're working on a Qt C++ Widget project and recently ve'we run into trouble. 我们正在研究Qt C ++ Widget项目,最近遇到了麻烦。 We're Qt rookies. 我们是Qt新秀。

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... 在我们的小部件上,有两个QGraphicsView ,它们需要在调整窗口大小时自动调整大小(我们已经完成了此操作),并将其中的内容相应地调整大小/适合/调整为...

So we've figured we need to somehow hook to onResizeEvent or find which slot does it. 因此,我们认为我们需要以某种方式挂接到onResizeEvent或查找执行该操作的插槽。 But we're somehow lost as to how to do it. 但是我们在某种程度上迷失了如何去做。

PS: Please excuse my english. PS:请原谅我的英语。

If you have a QWidget you only need to reimplement the resizeEvent function. 如果您有QWidget,则只需重新实现resizeEvent函数。

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 在Qt文档中,您可以找到完整代码示例的涂鸦示例: http : //doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html

Alternatively you can install an event handler and grab the QEvent::Resize . 或者,您可以安装事件处理程序并获取QEvent::Resize How to install event filters is described here: http://qt-project.org/doc/qt-4.8/eventsandfilters.html 此处介绍了如何安装事件过滤器: http : //qt-project.org/doc/qt-4.8/eventsandfilters.html

You can make your custom class which inherits from QGraphicsView. 您可以使您的自定义类继承自QGraphicsView。 You should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like: 您应该在自定义QGraphicsView中重新实现resizeEvent(QResizeEvent * event),如下所示:

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. 即,如果更改了窗口大小并且调整了graphicsView的大小,则场景将被缩放,并且您可以适当地看到所有内容。

If you are resizing in some slot then you can use sender() . 如果要在某个插槽中调整大小,则可以使用sender() Btw you can always use debugger or logs/backtraces to get such information. 顺便说一句,您始终可以使用调试器或日志/回溯获取此类信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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