Results 1 to 13 of 13

Thread: paintEvent not getting called with QMainWindow

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default paintEvent not getting called with QMainWindow

    I have a class that extends QMainWindow and I overrode the paintEvent method, but it never gets called. I have done something similar when extending QWidget and it does call the method. A little confused here... it seems to be declared correctly:

    protected:
    virtual void paintEvent(QPaintEvent*);
    ....
    ....
    void MyWindow:: paintEvent(QPaintEvent*)
    {
    // never gets here
    }
    Last edited by DiamonDogX; 9th April 2009 at 19:14.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: paintEvent not getting called with QMainWindow

    show us more code, please.

  3. #3
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: paintEvent not getting called with QMainWindow

    Qt Code:
    1. class MyWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MainWindow();
    7. virtual ~MainWindow();
    8.  
    9. protected:
    10. virtual void paintEvent(QPaintEvent*);
    11. ....
    12. ....
    13. };
    14.  
    15. // Class implementation:
    16. void MyWindow:: paintEvent(QPaintEvent*)
    17. {
    18. // Never gets called
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 9th April 2009 at 23:51. Reason: missing [code] tags

  4. #4
    Join Date
    Feb 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent not getting called with QMainWindow

    Remove virtual from paintEvent definition and retry

  5. #5
    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: paintEvent not getting called with QMainWindow

    How do you know it never gets called?
    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.


  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: paintEvent not getting called with QMainWindow

    Quote Originally Posted by sorin View Post
    Remove virtual from paintEvent definition and retry
    why? do you think it solves the problem? it's not mistake.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: paintEvent not getting called with QMainWindow

    I think he is trying to draw something and he expects that the image should appear on central widget. it's just a guess. if I'm right, then you need:
    * install event filter on central widget,
    * subclass a QWidget, override paintEvent and then set this widget as central widget on QMainWindow.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: paintEvent not getting called with QMainWindow

    Yes, spirit, you're pretty much right. Being relatively new to Qt, I am discovering its quirks. I guess by design the paintEvent will not get called in my case of the QMainWindow. I understand that my event filter should capture events from the central widget, but what type of code would I need in my eventFilter() method to ensure this works, i.e. gets painted?

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: paintEvent not getting called with QMainWindow

    you should do like this
    Qt Code:
    1. bool MyMainWindow::eventFilter(QObject *o, QEvent *e)
    2. {
    3. if (o == centralWidget() && e->type() == QEvent::Paint) {
    4. //paint on central widget
    5. ...
    6. }
    7. return QMainWindow::eventFilter(o, e);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #10
    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: paintEvent not getting called with QMainWindow

    Quote Originally Posted by DiamonDogX View Post
    I guess by design the paintEvent will not get called in my case of the QMainWindow.
    It is called. You just don't see its result because something else paints over it.
    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.


  11. #11
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: paintEvent not getting called with QMainWindow

    How can you say its called or not. I am in the same situation and I simply put a breakpoint in the function and the program never reached the breakpoint therefor it is not getting called.

  12. #12
    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: paintEvent not getting called with QMainWindow

    You just check if it is called:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyWindow : public QMainWindow {
    4. public:
    5. MyWindow(){}
    6. protected:
    7. void paintEvent(QPaintEvent *pe){
    8. qDebug() << Q_FUNC_INFO;
    9. QMainWindow::paintEvent(pe);
    10. }
    11. };
    12.  
    13. int main(int argc, char **argv){
    14. QApplication app(argc, argv);
    15. MyWindow mw;
    16. mw.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    In this example it is called.
    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.


  13. #13
    Join Date
    Dec 2009
    Posts
    29
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: paintEvent not getting called with QMainWindow

    In my case, this was happening with a QFrame. Here is the solution if anyone ever needs it:

    Qt Code:
    1. rgbFrame::rgbFrame(QWidget * parent, Qt::WindowFlags f): QFrame(parent, f)
    2. {
    3. installEventFilter(this);
    4. }
    5.  
    6. void rgbFrame::paintEvent(QPaintEvent *e)
    7. {
    8. QFrame::paintEvent(e); // pass event to base class
    9. }
    10.  
    11.  
    12. bool rgbFrame::eventFilter(QObject *o, QEvent *e)
    13. {
    14. if (e->type() == QEvent::Paint) {
    15. paintEvent((QPaintEvent *)e);
    16. }
    17. return QFrame::eventFilter(o, e);
    18. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 7th August 2008, 14:46
  2. QMainWindow setCentralWidget from ui widget, Qt4
    By alan in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2008, 14:00
  3. QMainWindow child of a QDialog
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2008, 08:16
  4. Replies: 3
    Last Post: 27th November 2006, 10:56
  5. QMainWindow not receiving QResizeEvent
    By brcain in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 07:11

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.