连接到派生类中的 Qt 信号
我正在构建一个基于 Box2D 的物理沙箱并使用 Qt 作为 UI。我有一个 QGraphicsScene (Box2DView) 的派生类,用于处理鼠标和键盘事件。我想连接到 sceneRectChanged 信号 (void QGraphicsScene::sceneRectChanged ( const QRectF & rect ) [signal]
),但我不知道如何引用鉴于我的实例属于派生类。
我尝试按如下方式连接它:
connect(view, SIGNAL(sceneRectChanged( const QRectF & )),
this, SLOT(debugViewChanged()) );
其中 view
是 Box2DView 的实例。我收到以下运行时错误:
Object::connect:没有这样的信号 Box2DView::sceneRectChanged( const QRectF & )
但是,当我尝试覆盖 Box2DView 中的 setRectChanged 时,如下所示:
void Box2DView::sceneRectChanged () {
QGraphicsScene::sceneRectChanged ( this->rect() );
}
我收到编译时错误
无法在没有对象的情况下调用成员函数“void QGraphicsScene::sceneRectChanged(const QRectF&)”
我知道我的重写方法不正确,但不知道该怎么做。是否有不同的语法来引用基类的 Qt 信号?
编辑:解决方案出现在问题的评论中。我混淆了场景和视图。我的 Box2DView 类确实是从 QGraphicsView 派生的,因此解决方案是将连接更改为信号的引用 view->scene()
。
I'm building a physics sandbox based upon Box2D and using Qt for the UI. I have a derived class of QGraphicsScene (Box2DView) that handles mouse and keyboard events. I would like to connect to the sceneRectChanged signal (void QGraphicsScene::sceneRectChanged ( const QRectF & rect ) [signal]
), but I don't know how to reference it given that my instance is of the derived class.
I've tried to connect it as follows:
connect(view, SIGNAL(sceneRectChanged( const QRectF & )),
this, SLOT(debugViewChanged()) );
where view
is an instance of the Box2DView. I get the following runtime error:
Object::connect: No such signal Box2DView::sceneRectChanged( const QRectF & )
However, when I try to override setRectChanged in Box2DView, as follows:
void Box2DView::sceneRectChanged () {
QGraphicsScene::sceneRectChanged ( this->rect() );
}
I get the compile-time error
cannot call member function ‘void QGraphicsScene::sceneRectChanged(const QRectF&)’ without object
I know that my override method isn't right, but can't figure out what to do. Is there a different syntax to reference a base class's Qt signal?
EDIT: The solution appeared in the comments to the question. I was confusing the Scene and the View. My Box2DView class is indeed derived from QGraphicsView, so the solution was to change the connection to reference view->scene()
for the signal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论