问题描述
我想知道是否有办法做到这一点。
I am wondering if there is a way to do this.
- 我创建了一个Qt应用程序(使用Creator 3.6.1,Qt 5.6.0)。
- 我在主窗口中添加了一个小部件。例如,一个名为myView的QGraphicsView。
- 我创建了一个从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小部件和派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!