PDA

View Full Version : change the painter of parent.



weixj2003ld
9th August 2011, 08:45
I draw a polygon int the paintEvent of class A,and create class B based on A.

Now I want to fill the polygon of A in the paintEvent of B,how to do?

wysota
9th August 2011, 10:25
Provide a virtual method in A that is called during paint event of A and reimplement that method in B.

weixj2003ld
9th August 2011, 15:21
Thk u for your answer.But My problem is still exists.
Now I paste my code on the page,




class A :public QLabel
//.h
{
Public
A(QWidget *parent=0);
void virtual paintEvent(QPaintEvent *);
};
class B:public A
{
public
B(QWidget *parent=0);
void paintEvent(QPaintEvent *)
};

/*.cpp*/

A::A();
void A::paintEvent(QPaintEvent *)
{
/*Here,Draw a Polygon,but not filled.*/
}
B::B(QWidget *parent):A(parent)
{
}
void B::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.SetBrush(Qt::BrushStyle::SolidPattern);
A::paintEvent(e);
}

Why?

wysota
10th August 2011, 08:24
Why what? This is not what I told you to do...


class A :public QLabel
//.h
{
public:
A(QWidget *parent=0);
protected:
void virtual paintEvent(QPaintEvent *);
virtual void setupPainter(QPainter*) {}
};

class B:public A
{
public:
B(QWidget *parent=0);
protected:
void setupPainter(QPainter *p) {
p->setBrush(...);
}
};

/*.cpp*/

A::A();
void A::paintEvent(QPaintEvent *)
{
QPainter p(this);
setupPainter(&p);
p.draw....
}
B::B(QWidget *parent):A(parent)
{
}