tags:

views:

521

answers:

1

I have a simple hierarchy of widgets: GraphWidget -> MotionWidget -> NodeWidget. I am new to Qt, so I am not quite sure about how some insides work yet. Basically, GraphWidget creates a single MotionWidget M and sets M's parent to itself. M then goes away and creates a bunch of NodeWidgets. However, NodeWidgets never get painted nor does their paintEvent() function gets called.

I tried creating MotionWidget directly, without GraphWidget and everything works. So why does things break if I add GraphWidget to the hierarchy?

Here is a paste with the relevant bits of code from my project. I also included the output from GraphWidget::dumpObjectTree() at the top.

Edit: forgot to include the paste link ;) http://rafb.net/p/Zp39CF94.html

Update: I wrapped MotionWidget into a layout.

Before:

GraphWidget :: GraphWidget( QWidget *parent ) : QWidget( parent )
{
    setFixedSize( 500, 500 );

    MotionWidget *n = new MotionWidget( 5, this );
}

After

GraphWidget :: GraphWidget( QWidget *parent ) : QWidget( parent )
{
    setFixedSize( 500, 500 );

    QVBoxLayout *l = new QVBoxLayout;

    MotionWidget *n = new MotionWidget( 5 );

    l->addWidget( n );

    setLayout( l );
}

Now, the latter works. I.e. everything gets drawn. The question then becomes... Why? Why didn't it work in the first case but worked in the second?

+1  A: 

I'm going to disagree with Aiua's answer. Passing the parent to an object will add the child to the visual hierarchy. (Otherwise, using Designer to make a widget including others, but with absolute positioning, wouldn't work.) However, there were probably other factors keeping the painting from happening.

Due to the fact that the layout made things work, I'm going to guess that in both versions, MotionWidget has no set size. That would mean in the first example that it would be sized to 0x0 pixels, which Qt will optimize out when trying to paint. In the second example, the layout controls the size of the widgets added to it, and so would enlarge MotionWidget, and thus it would start to receive paint events again.

To fix this, you could set the size explicitly like you do for GraphWidget, or you could do some dynamic sizing based on the parent's size, or GraphWidget could control the size, or any of a large number of various options. Layouts can be nice also, but aren't absolutely required.

Caleb Huitt - cjhuitt
You're absolutely right. Sorry for my bad answer. I never use absolute positioning so I forgot that in that case it uses the parent widget for the visual hierarchy.
Aiua