c++ - Qt QGraphicsEllipseItem subclass doesn't display -


I am trying to create a subclass of QGraphicsEllipseItem so that I can add some functionality, Even in a very basic exam, subclass objects are not displayed in the scene. In the following code, the blue circle is displayed in (- 30,0) , but there is no red circle on (30,0) . What am I doing wrong?

In researching this problem, I noticed that the color is usually overridden for QGraphicsItem subclasses, so I tried that, only QGraphicsEllipseItem :: paint < Calling in / code> myEllipseItem :: paint , but it did not make any difference.

main.cpp

  #include "widget.j" # include & lt; QApplication & gt; Int main (int argc, char * argv []) {Q Application A (argc, argv); Widget w; W.show (); Back a.exec (); }  

widget.h

  #ifndef WIDGET_H #define WIDGET_H #include & lt; QWidget & gt; # Include & lt; QGraphicsScene & gt; # Include & lt; QGraphicsView & gt; # Include & lt; QGraphicsItem & gt; Class widget: public QWidget {Q_OBJECT public: widget (QWidget * parent = 0); ~ Widget (); Private: QGraphicsScene * View; }; Class myEllipseItem: Public QGraphicsEllipseItem {Public: myEllipseItem (Float A, Float B, Float C, Float D) {QGraphicsEllipseItem (a, b, c, d);}}; #endif // WIDGET_H  

widget.cpp

#include "widget.h" #include & lt; QLayout & gt; Widget :: Widget (QWidget * Basic): QWidget (Guardian) {View = New QGraphicsScene (); QGraphicsEllipseItem * qIcon = New QGraphicsEllipseItem (0.0, 0.0, 20.0, 20.0); QIcon-> SetBrush (QBrush (Qt :: blue)); Scene-> AddItem (qIcon); QIcon-> SetPos (-30,0); MyEllipseItem * myIcon = new myEllipseItem (0.0, 0.0, 20.0, 20.0); MyIcon-> SetBrush (QBrush (QT :: Red)); Scene-> AddItem (myIcon); MyIcon-> SetPos (30,0); QGraphicsView * View = New QGraphicsView (); View-> SetScene (view); QGridLayout * Layout = New QGridLayout; Layout & gt; AddWidget (see); SetLayout (layout); } Widget: ~ widget () {}

Your constructor is not right. You should use next (in my example it has an empty body but you can do all the necessary things):

Header:

  #ifndef MYELLIPSEITEM_H #define MYELLIPSEITEM_H #include & Lt; QGraphicsEllipseItem & gt; Category myEllipseItem: Public QGraphicsEllipseItem {Public: myEllipseItem (Float A, Float B, Float C, Float D): QGraphicsEllipseItem (a, b, c, d) {}}; #endif // MYCOLORDIALOG_H  

And as you can see, I used different header files how to use the constructor's body:

  MyEllipseItem (Float A, Float B, Float C, Float D): QGraphicsEllipseItem (a, b, c, d) {this-> Set brush (QBrush (QT :: green)); }  

Now the "default" color in the oval is green.


Comments