2

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;
...
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mister Rose
  • 117
  • 3
  • 11

1 Answers1

3

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • promote the base class to become a derived class? I'm confused. I have a `calculator.ui`, `calcuator.cpp`, `calcuator.h` that I want to use as a base class for my `scientific_calculator.cpp` so promote the calculator class to become a scientific_calculator? – mLstudent33 May 30 '20 at 21:30
  • I mean, rather than a promotion, which I see as a lower derived class promoted to a higher base class, this seems to do the opposite. – mLstudent33 May 30 '20 at 21:40