PDA

View Full Version : Widget does not update



Caius Aérobus
31st December 2007, 12:48
I have probably (surely?) made a mistake somewhere but...impossible to find it out! Could you help please?



class CircleParameter : public QWidget
{
Q_OBJECT

public:
CircleParameter(QWidget *parent=0);
virtual ~CircleParameter() {};

public slots:
void setRadius(int radius);

protected:
void paintEvent(QPaintEvent *e);

private:
int radius;
};

CircleParameter::CircleParameter(QWidget *parent)
: QWidget(parent)
{
printf("creating widget\n");
this->radius = DEFAULT_RADIUS;
this->resize(MAX_RADIUS*2, MAX_RADIUS*2);
if (updatesEnabled()) printf("yes\n"); else printf("no\n");
if (isVisible()) printf("yes\n"); else printf("no\n");
}

void
CircleParameter::setRadius(int radius)
{
printf("setRadius()\n");
if (updatesEnabled()) printf("yes\n"); else printf("no\n");
if (isVisible()) printf("yes\n"); else printf("no\n");
this->radius = radius>0 ? radius<=MAX_RADIUS ? radius : MAX_RADIUS : 1;
this->update();
}

void
CircleParameter::paintEvent(QPaintEvent *e)
{
printf("paintEvent()\n");
QPainter painter(this);
QPen pen;
pen.setColor(Qt::red);
painter.setPen(pen);
painter.drawEllipse(this->size().width()/2-this->radius, this->size().height()/2-this->radius, radius*2, radius*2);
}


Outputs:
- at creation time:


creating widget
yes
no

- when moving a slider connected to CircleParameter::setRadius():


setRadius()
yes
yes


Issue: why CircleParameter:: paintEvent() never executed?

marcel
31st December 2007, 12:52
If the CircleParameter widget is inside a layout, calling resize() will not work. You have to set a fixed size for your widget.

The paint event probably doesn't get called because the widget size is (0, 0).
Try printing this->size() in setRadius. It will most likely be (0, 0);

Caius Aérobus
2nd January 2008, 23:26
If the CircleParameter widget is inside a layout, calling resize() will not work. You have to set a fixed size for your widget.


I did not know about this behaviour of widgets in a layout. It works now. Thank you very much for your help!