Qt小部件和派生类 [英] Qt Widgets and derived classes

查看:113
本文介绍了Qt小部件和派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法做到这一点。

I am wondering if there is a way to do this.


  1. 我创建了一个Qt应用程序(使用Creator 3.6.1,Qt 5.6.0)。

  2. 我在主窗口中添加了一个小部件。例如,一个名为myView的QGraphicsView。

  3. 我创建了一个从QGraphicsView派生的C ++类(称为DerivedView)

DerivedView类的代码:

code of DerivedView class:

class DerivedView : public QGraphicsView {
...

我希望新的DerivedView类可以控制此小部件。我可以通过ui-> myView访问指向该对象的指针。有什么方法可以使我的派生类与已实例化的QGraphicsView一起使用?

I would like my new DerivedView class to control this widget. I can access a pointer to the object through ui->myView. Is there any way to do get my derived class to work with the already instantiated QGraphicsView?

DerivedView * dView = ui->myView;

还是我不需要从QGraphicsView派生我的类,而只需将指针添加为数据成员?

Or do I need to not derive my class from QGraphicsView and just add a pointer as a data member?

class DerivedView {
    QGraphicsView * gv;
...


推荐答案

您应该宣传自己的 QGraphicsView DerivedView ,为此,请执行以下步骤。

You should promote your QGraphicsView to DerivedView, for this follows the following steps.

右键单击 QGraphicsView 并选择 promote to ..

并添加类和标题的名称

,然后按添加。

然后按促进。
之后, ui-> myView 已经是 DerivedView 类的成员

And then press on promote. After this, ui->myView is already a member of the DerivedView class

这篇关于Qt小部件和派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆