Search code examples
c++qtqpainter

Qt paint figures at one window


  1. I create class Widget, it creates window, this class paints something on the window (i.e. it works as I want).
  2. I create yet one class, Circle, I want to paint on the window of class Widget.
  3. I pass adress of Widget and try to paint on Widget using QPainter paint (address of Widget); (in the instance of Circle) but i don't see anything.

I've tried to make code as shorter as possible during the execution of program I type out address of object Widget. It doesn't change. It means that the address of Widget was passed right.

Everywhere, where I type out address of Widget I receive the same address. Here is the code:

header Widget

            class Widget : public QWidget
            {
                public:
                int  mi,mcount;
                Widget(QWidget *parent = 0);
                QPaintEvent *ev;
                virtual void paintEvent(QPaintEvent *);
            void drawcircle();
            };

Widget.cpp

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    QWidget::paintEvent(ev);

    qDebug()<<this<<"\n";  //
}

        void Widget::drawcircle()
        {
        QPainter paint(this);
        paint.drawEllipse(0,0,100,100);
        }


void Widget::paintEvent(QPaintEvent *ev)
{    this->drawcircle(); }

header Circle.h

        class Circle :public QWidget
        {
            public:
            Circle(Widget *widget);    // i do trick here!!!
            Widget *mwidg;
        QPaintEvent *ev;

        virtual void paintEvent(QPaintEvent *);
        void drawcircle(Widget *mwidg);
        };

Circle.cpp

    Circle::Circle(Widget *widget)
        {
        qDebug()<<"circle widget"<<widget;
        QWidget::paintEvent(ev);
        mwidg=widget;
        qDebug()<<"\n"<<mwidg;
        }



    void Circle::paintEvent(QPaintEvent *ev)
    {  qDebug()<<"circle paintEvent mwidget"<<mwidg<<"\n";
    this->drawcircle(mwidg);
        }


        void Circle::drawcircle(Widget *mwidg)
            {
                QPainter paint(mwidg);
                paint.drawEllipse(20,10,40,40);
            paint.drawLine(0,0,500,500);
            }

main

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget *w=new Widget;

            qDebug()<<"main address of widget"<<w<<"\n";
        Circle *f=new Circle(w);
        w->show();

        return a.exec();
        }

program is compiled and linked successful


Solution

  • well , thank you for your attempts to help but all that i was needing:

    this -> setParent(widget);

    in costructor Circle::Circle, if somebody'll want to see my solve,one can see that figures are moved

    source code is here source code

    enter image description here