Qt Widgets and derived classes

我怕爱的太早我们不能终老 提交于 2019-12-18 09:44:07

问题


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

  1. I create a Qt Application (using Creator 3.6.1, Qt 5.6.0).
  2. I add a widget to the main window. For example a QGraphicsView called myView.
  3. I create a C++ class derived from QGraphicsView (called DerivedView)

code of DerivedView class:

class DerivedView : public 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;

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

class DerivedView {
    QGraphicsView * gv;
...

回答1:


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

Right click on QGraphicsView and select promote to ..:

And add the name of the class and header

And press add.

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



来源:https://stackoverflow.com/questions/41577262/qt-widgets-and-derived-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!