Results 1 to 4 of 4

Thread: disuse the father class's paintevent and reimplement it completely

  1. #1
    Join Date
    Nov 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question disuse the father class's paintevent and reimplement it completely

    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.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: disuse the father class's paintevent and reimplement it completely

    Just reimplement the paintEvent.

    void QWidget:aintEvent ( QPaintEvent * event )

  3. #3
    Join Date
    Nov 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: disuse the father class's paintevent and reimplement it completely

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: disuse the father class's paintevent and reimplement it completely

    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:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPainter>
    4.  
    5. class MyLabel : public QLabel {
    6. public:
    7. MyLabel(QWidget *p=0) : QLabel(p){}
    8. protected:
    9. void paintEvent(QPaintEvent *){
    10. QPainter p(this);
    11. p.drawText(rect(), Qt::AlignCenter, "X");
    12. }
    13. };
    14.  
    15. int main(int argc, char **argv){
    16. QApplication app(argc, argv);
    17. MyLabel lab;
    18. lab.setText("Some completely custom text");
    19. lab.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.