Results 1 to 7 of 7

Thread: why override paint just to drawPrimitive(PE_Widget)?

  1. #1
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default why override paint just to drawPrimitive(PE_Widget)?

    Why would someone override paintEvent to just do this:


    Qt Code:
    1. opt.init(this);
    2. QPainter p(this);
    3. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: why override paint just to drawPrimitive(PE_Widget)?

    Because paintEvent gets called whenever painting - or re-painting - is required. Stick an output statement in paintEvent and start dragging windows partially offscreen, obscuring them with other windows, hiding and showing them and other activities; watch your console fill up with output.

  3. #3
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why override paint just to drawPrimitive(PE_Widget)?

    Quote Originally Posted by SixDegrees View Post
    Because paintEvent gets called whenever painting - or re-painting - is required. Stick an output statement in paintEvent and start dragging windows partially offscreen, obscuring them with other windows, hiding and showing them and other activities; watch your console fill up with output.
    Yes, I know all that. The question is, why would someone override the paintEvent inherited from QWidget just to tell the system to draw this widget. QWidget already has a paintEvent that I assume draws the widget and its children (seems to work fine), so why would it be necessary to write this override? In other words, what problem is this override written to solve?


    Added after 19 minutes:


    I stepped through both the OP method and simply calling QWidget:aintEvent(). Now I'm even more confused because neither one does anything. QWidget's paintEvent is an empty block and when you pass PE_Widget to drawPrimitive and the last parameter is directly derived from QWidget, all executable code is skipped.
    Last edited by nroberts; 23rd December 2010 at 17:29.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: why override paint just to drawPrimitive(PE_Widget)?

    A QWidget object isn't visible; it has nothing to paint. Unlike the many widgets which derive from it.

  5. #5
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why override paint just to drawPrimitive(PE_Widget)?

    Quote Originally Posted by SixDegrees View Post
    A QWidget object isn't visible; it has nothing to paint. Unlike the many widgets which derive from it.
    I'm afraid I don't understand your point; could you please explain?


    Added after 1 40 minutes:


    Quote Originally Posted by nroberts View Post
    Yes, I know all that. The question is, why would someone override the paintEvent inherited from QWidget just to tell the system to draw this widget. QWidget already has a paintEvent that I assume draws the widget and its children (seems to work fine), so why would it be necessary to write this override? In other words, what problem is this override written to solve?


    Added after 19 minutes:


    I stepped through both the OP method and simply calling QWidget:aintEvent(). Now I'm even more confused because neither one does anything. QWidget's paintEvent is an empty block and when you pass PE_Widget to drawPrimitive and the last parameter is directly derived from QWidget, all executable code is skipped.
    I suppose it might help if I explain WHY I am so interested in this piece of apparently useless code that someone went out of their way to make.

    The code in question is a part of a qt-solutions library. Perhaps I am mistaken in assuming that the authors of such libraries would have any special expertise in Qt (especially given that the library in question is so poorly made that I decided to make my own version of it), but I would hope that it's reasonable to assume there's at least a little peer review of such officially recognized extensions (they used to charge for them as I understand).

    So what I'm trying to figure out is why someone that has intimate knowledge of Qt would do something like this. I'm trying to learn Qt specific methods from this library's source code and this particular piece is just throwing me off. Did they really just write a bunch of redundant code that does nothing for no reason, or was there something guiding their purpose? Legacy? Future?
    Last edited by nroberts; 27th December 2010 at 21:03.

  6. #6
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: why override paint just to drawPrimitive(PE_Widget)?

    Hi,
    we using the same code in our solution for correctly painting our own implementations of QWidgets if they have a stylesheet. If you do not the stylesheet commands are not applied in your own widgets. Really nasty!

    primat

  7. #7
    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: why override paint just to drawPrimitive(PE_Widget)?

    Quote Originally Posted by theprimat View Post
    If you do not the stylesheet commands are not applied in your own widgets.
    Same goes for any custom style. The fact that default styles draw empty widgets as 'nothing' doesn't mean custom styles (like stylesheets) have to follow that pattern as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Can't override the closeEvent()
    By scot_hansen in forum Newbie
    Replies: 7
    Last Post: 5th October 2010, 22:47
  2. How to override ShowEvent() etc
    By marcvanriet in forum Newbie
    Replies: 1
    Last Post: 22nd September 2010, 10:13
  3. Shortcut override ?
    By fenerista in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2010, 12:02
  4. Implementing WndProc Override
    By bbdaffy in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2010, 20:37
  5. Override a signal
    By daviddoria in forum Newbie
    Replies: 4
    Last Post: 9th February 2010, 18:18

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.