Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

c++ - Qt Widgets and derived classes

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;
...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Right click on QGraphicsView and select promote to ..:

enter image description here

And add the name of the class and header

enter image description here

And press add.

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...