PDA

View Full Version : paintEvent not getting called with QMainWindow



DiamonDogX
9th April 2009, 17:00
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
}

caduel
9th April 2009, 19:23
show us more code, please.

DiamonDogX
9th April 2009, 22:46
class MyWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
virtual ~MainWindow();

protected:
virtual void paintEvent(QPaintEvent*);
....
....
};

// Class implementation:
void MyWindow:: paintEvent(QPaintEvent*)
{
// Never gets called
}

sorin
9th April 2009, 22:50
Remove virtual from paintEvent definition and retry

wysota
9th April 2009, 23:51
How do you know it never gets called?

spirit
10th April 2009, 07:19
Remove virtual from paintEvent definition and retry

why? do you think it solves the problem? it's not mistake.

spirit
10th April 2009, 07:23
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.

DiamonDogX
10th April 2009, 17:20
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?

spirit
10th April 2009, 17:27
you should do like this


bool MyMainWindow::eventFilter(QObject *o, QEvent *e)
{
if (o == centralWidget() && e->type() == QEvent::Paint) {
//paint on central widget
...
}
return QMainWindow::eventFilter(o, e);
}

wysota
10th April 2009, 18:39
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.

yodasoda
15th June 2011, 22:58
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.

wysota
15th June 2011, 23:04
You just check if it is called:

#include <QtGui>

class MyWindow : public QMainWindow {
public:
MyWindow(){}
protected:
void paintEvent(QPaintEvent *pe){
qDebug() << Q_FUNC_INFO;
QMainWindow::paintEvent(pe);
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
MyWindow mw;
mw.show();
return app.exec();
}

In this example it is called.

yodasoda
15th June 2011, 23:23
In my case, this was happening with a QFrame. Here is the solution if anyone ever needs it:


rgbFrame::rgbFrame(QWidget * parent, Qt::WindowFlags f): QFrame(parent, f)
{
installEventFilter(this);
}

void rgbFrame::paintEvent(QPaintEvent *e)
{
QFrame::paintEvent(e); // pass event to base class
}


bool rgbFrame::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::Paint) {
paintEvent((QPaintEvent *)e);
}
return QFrame::eventFilter(o, e);
}