Q:

How to update sizeHint of child widget

I have a parent widget (retangular shape) and I want to place a child widget inside it. The position of the child should be configurable. The child is dynamically added to the parent widget when a button is pushed. When the button is pushed an instance of ChildWidget is created.
ChildWidget::ChildWidget(QWidget* pParent)
{
    setParent(pParent)

    show();

    SetChildPosition();

}

/*********** Protected methods ************/

void ChildWidget::paintEvent(QPaintEvent* pEvent)
{
   QPainter painter(this);
   painter.drawRect(0, 0, sizeHint().width() - 1, sizeHint().height() - 1);
   resize(sizeHint());

   QWidget::paintEvent(pEvent);
}

/*********** Private methods ************/

void ChildWidget::SetChildPosition()
{
   QPoint childPos = QPoint(parentWidget()->width() - sizeHint().width() - 5, 5);

   move(childPos);
}

At the moment the PaintEvent is called, the sizeHint() is correct (the rectangle drawn has the size of the widget. However it seems that sizeHint().width() = -1 in the "SetChildPosition" function. Is there any way to update the sizehint before I call the "SetChildPosition"?

A:

I have solved the problem by using a layout as suggested.
I placed a QGridLayout on the parentWidget and placed the child widget inside of it. Then I used the 'setColumnMinimumWidth' and 'setRowMinimumHeight' of the QGridLayout to get it at the right pixel coordinate.

