0
  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

4

2 に答える 2

0

さて、あなたの助けの試みに感謝しますが、私が必要としていたすべてのもの:

this-> setParent(widget);

costructor Circle :: Circleで、誰かが私の解決策を見たいと思うなら、数字が動かされているのを見ることができます

ソースコードはこちら ソースコード

ここに画像の説明を入力してください

于 2011-05-31T14:12:59.877 に答える
0

正確に何を達成しようとしていますか?独自の paintEvent() ハンドラーでのみ任意のウィジェットにペイントできます。自分で paintEvent() を呼び出さないでください。機能しません。また、QPaintEvent メンバー変数を取り除きます。

Circle を Widget の子にして、Circle::paintEvent() から円をペイントすることをお勧めします。または、QGraphicsView を使用します。

于 2011-05-30T09:58:59.810 に答える