Question

I have a QWidget based class that I need to instantiate from another object.

If I create the widget in the body of its parent widget class it is all good:

new NodeWidget(rootItem, this); // this works

But when I use the aux method the widget is created, but never drawn.

rootItem->createWidget(this); // internally rootItem does the same thing as above

The method itself:

void createWidget(QWidget * target) {
     if (!_ui) _ui = new NodeWidget(this, target);
...
}

The way I see it, both approaches do the same thing, but the first one works while the second one does not. The NodeWidget constructor runs, but the widget does not appear in the parent widget and the paintEvent is never called. Any ideas why?

EDIT: This is certainly odd, I noticed that the following line:

new QPushButton(this);

works when called in the constructor of the parent widget, but not when called from the mousePress event. What IS the difference?

Was it helpful?

Solution

Do you call show() for your custom widget?

If the child widget is created after it's parent is already showed the parent-child relationship can't show the child too, so you need to explicitly call show().

But when the widget is added in the constructor, the parent isn't showed yet and then when the parent is showed it automatically shows all the children that were not explicitly hidden.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top