PDA

View Full Version : disuse the father class's paintevent and reimplement it completely



zwngcn
27th November 2006, 07:15
Hi all,

In almost all the concrete widgets such as QLabel and QPushButton, the function of paintevent uses a private member QXXXXPrivate, so is it possible to subclass widget and reimplement paintevent by not using the father class's paintevent?

For example, a subclass MyLabel inherited from QLable, I want to the MyLabel be like this: every character of the label text is shown in differnt color. In this case, the QLabel's paintevent is not useful. You have to use the information of QLabelPrivate and reimplement the paintevent completely, so it is hard to reimplement paintevent.

Maybe I am wrong. But if it is hard to reimplement paintevent, why Qt is designed like that? Although subclassing from QWdiget is simple, someone want to subclass from concrete widget and draw the widget unlike to its father widget.

Thanks for your help.

munna
27th November 2006, 07:22
Just reimplement the paintEvent.

void QWidget::paintEvent ( QPaintEvent * event ) (http://doc.trolltech.com/4.2/qwidget.html#paintEvent)

zwngcn
27th November 2006, 07:40
But I want to every character of the MyLabel' text is shown in different color.
And the Point is that no using the father's paintevent. It means the MyLabel's paintevent Starts over from the beginning.

Thanks.

wysota
27th November 2006, 09:56
If you subclass and reimplement paintEvent and don't call the base class implementation of the paintEvent from your implementation explicitely, then your handler will replace the base class event handler completely.

Look at this:

#include <QApplication>
#include <QLabel>
#include <QPainter>

class MyLabel : public QLabel {
public:
MyLabel(QWidget *p=0) : QLabel(p){}
protected:
void paintEvent(QPaintEvent *){
QPainter p(this);
p.drawText(rect(), Qt::AlignCenter, "X");
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
MyLabel lab;
lab.setText("Some completely custom text");
lab.show();
return app.exec();
}

No matter what you set using QLabel::setText() you'll always see just an "X".