PDA

View Full Version : strange behavior of paintEvent



hashb
30th August 2010, 03:27
platform :QT4.6,QT4.7 linux

Hi, All,

I got a very strange problem about paintEvent.
Here is my code



....
status = true; //set status to true by default
....

void MainWindow::paintEvent(QPaintEvent * event )
{
qDebug()<<"paintEvent";

QMainWindow::paintEvent(event);
QPainter p(this);
if(status)
p.drawText(110,110,"hello");
else
p.drawText(110,110,"world");
}

void MainWindow::on_pushButton_clicked()
{
if(!status)
status = true;
else
status = false;
}


Here is my steps:
1. start app , on the window it shows "hello"
2. click button, status changed to false
3. move the window to the edge of the screen ,until it become invisible.
4. move the window back to the center of the screen
5. from debug info,I can see that paintEvent has been called,
but what puzzled me is that in the window, it still shows "hello" !!! :confused:
6. minimize the window and the raise it again, the window also still shows "hello" :confused:
7. resize the window ,now it shows "world"

any ideas about it ?
Thanks in advance for your help.

Best regards,
hb

tbscope
30th August 2010, 05:36
I guess status is a member variable of MainWindow?
Can you put the value of status in the paint event debug message and see what it actually is?

For you clicked slot, you can also write this:

void MainWindow::on_pushButton_clicked()
{
status = !status;
update(); // updates the widget
}

hashb
30th August 2010, 06:40
Hi tbscope,

it works after add update(), Thanks a lot :)
but I am still wondering the issue why resize can refresh the window but others can not, in all cases the paintEvent have been called.

Best regards,
hb

wysota
30th August 2010, 07:48
It's because only the part of the widget that needs redrawing is actually redrawn (regardless of the code executed by the paint event). Check the rectangle contained in the QPaintEvent object to see what will actually be redrawn.