PDA

View Full Version : Updating variable



felo188
16th August 2011, 16:37
Hi. In my gui i have some buttons. For example when i move a mouse on IRButton1 and next IRButton2 i increase liczbaLudzi1. When i change a direction (from IRButton2 to IRButton1) decrease liczbaLudzi1. I want to display as many pixmaps (using labels) as it's value liczbaLudzi1. A part of program reliable for increase and decrease of liczbaLudzi1 is in filter event and it is working (increasing and decreasing as i wanted). I have problem with painting pixmaps. The problem is that if i increase liczbaLudzi1 i also increase number of pixmaps on gui (it's ok) but when i decrease liczbaLudzi1 the number of pixmaps stays on the last highest value liczbaLudzi1.


void MainWindow::paintEvent(QPaintEvent *paint)
{
...
switch (liczbaLudzi1)
{
case 1:
ludzik1->setVisible(true); update();break;
case 2:
ludzik2->setVisible(true); update();break;
case 3:
ludzik3->setVisible(true); update();break;
}
...
}
Where ludzik_ is QLabel.
What i should do to solve it. I try with update() but switch only works up (increase number of labels with increasing number of liczbaLudzi1) and don't works down (decrease number of labels with decreasing number of liczbaLudzi1).

stampede
16th August 2011, 17:08
I think its because you never call ludzikN->setVisible(false). When you show ludzikN, call setVisible(false) on each ludzik_N+1, ludzik_N+2 etc:


switch (liczbaLudzi1)
{
case 1:
ludzik1->setVisible(true); update();
// call setVisible(false) on ludzik2 and ludzik3 here
break;
case 2:
ludzik2->setVisible(true); update();
// same here for ludzik3
break;
case 3:
ludzik3->setVisible(true); update();break;
}

Another thing is that you can call "setVisible()" immediately when the liczbaLudzi1 value is changed, paintEvent() should only display the actual widget state, not change it.

yeye_olive
16th August 2011, 17:19
There are several problems with your approach:
1. paintEvent() implements the low-level painting of a widget. In such a function you should only use paint commands such as QPainter::drawPixmap(). Since you are using QLabels to display the images, forget about paintEvent() and call your code whenever the value of the variable changes.
2. It seems that you want to have exactly one QLabel visible at a time, which one being decided by the value of the variable. You correctly make the new QLabel visible, but you forget to hide the old one. Therefore once the frontmost QLabel becomes visible it hides all the others.
3. You use several QLabels when you could simply use one and change the pixmap it displays.

I thus recommend to have one QLabel and call setPixmap() with the appropriate pixmap whenever the value of the variable changes.

felo188
16th August 2011, 17:55
yeye_olive
Now i paste code in eventFilter, where liczbaLudziN is changing.

stampede
Of course! I didn't call setVisible(false) :)

Thanks for help.