PDA

View Full Version : force repaint



georgie
22nd May 2006, 12:11
I have reimplemented paintEvent for a number of my custom widgets

In a couple of places, I am having the problem that I need a repaint, even though the widget itself has not changed....this is because my paintEvent can depend upon settings which I have implemented elsewhere....(e.g. the user can choose one of a few radiobuttons which describe which colour they would like certain things to be, this radiobutton emits a signal which connects to a slot in the widget in question and this slot calls update()

I am definitely calling repaint()/update() (I have tried both) in the right places(AFIK)....but I think Qt must be optimizing and realizing that the widget itself has not in fact changed and thus does not call the paintEvent... it does not realise that the repaint depends on some third party info....and therefore does not update the colour until I change the value of the widget itself...

Is this expected behaviour? or have I missed a logical step in my signals and slots??

simplified code


QGroupBox* gb = new QGroupBox("Group Box");
QVBoxLayout* layout = new QVBoxLayout();
QRadioButton *radio1 = new QRadioButton("Red");
QRadioButton *radio2 = new QRadioButton("Green");
QRadioButton *radio3 = new QRadioButton("Blue");

layout->addWidget(radio1);
layout->addWidget(radio2);
layout->addWidget(radio3);
gb->setLayout(layout);

CustomWidget* cw = new CustomWidget();
connect(radio1, SIGNAL(clicked()), cw, SLOT(updateColour());
connect(radio2, SIGNAL(clicked()), cw, SLOT(updateColour());
connect(radio3, SIGNAL(clicked()), cw, SLOT(updateColour());




void CustomWidget::updateColour()
{
repaint(); //or update(); - I've tried both
}




void CustomWidget::paintEvent(QPaintEvent* event)
{ //notice that no members in the widget changed at all
if(blue was clicked)
change the palette to blue and call BaseClass::repaint();
else if(red was clicked)
as abve....usw....
}

munna
22nd May 2006, 12:15
From Qt docs:

void QWidget::repaint () [slot]

Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the widget is hidden.

We suggest only using repaint() if you need an immediate repaint, for example during animation. In almost all circumstances update() is better, as it permits Qt to optimize for speed and minimize flicker.

wysota
22nd May 2006, 12:58
If more than one paintEvents are pending in the event queue, they will be merged into one event which covers a region which is a sum of all regions requested for updating. But if the widget is visible, the paint event will take place, even if no members of the class are changed. I suggest you check if the updateColour() slot is being executed at all (for example by placing a qDebug() call in it).

georgie
22nd May 2006, 13:16
thanks for such fast replies!!


yeah....I'm getting the picture that the problem is with my SIGNAL/SLOT logic, not some rare time saver thing of Qt....=) (should be simpler to fix...i'll just wait till the morning when my brain is less fuzzy)

I will continue to look...I just wanted to check that I wasn't missing something in my understanding of the repaint() implementation

thankyou