Click to See Complete Forum and Search --> : Qt question. QGroupBox & QGridLayout.


A. Buza
04-04-2002, 12:27 AM
I've started using Qt about a week ago, and so far I like it a lot. However, I have a small problem. I have some widgets in a gridlayout that I'd like to add to a groupbox. I've looked at the examples, and this seems possible. I copied their process almost verbatim, but it segfaults when I run it.

The relevant code section is this:
QGroupBox* ugBox = new QGroupBox(this, "user btn group");
ugBox->setTitle(tr("User options"));


QGridLayout* usrBtnL = new QGridLayout( ugBox->layout() );


Anyone know what could be wrong with this? Thanks in advance for any advice anyone could give.

Edit: By the way, anyone know how to get emacs to format Qt specific stuff? It gets a little confused about "public slots:" and things of that nature.

[ 04 April 2002: Message edited by: A. Buza ]

bwkaz
04-04-2002, 10:08 AM
(g)vim does the same as emacs, I haven't tried to worry about it yet.

When you construct the QGridLayout, should you be passing it a parent widget? If so, then ugBox->layout() is probably NOT a widget, this might be it.

A. Buza
04-04-2002, 02:08 PM
Originally posted by bwkaz:
<STRONG>
When you construct the QGridLayout, should you be passing it a parent widget? If so, then ugBox-&gt;layout() is probably NOT a widget, this might be it.</STRONG>

That's what I thought, too. I looked at the different constructors for QGridLayout, and there isn't one that takes a QLayout, only QWidgets. However, they pass a QLayout to the constructor in the example program I was looking at ($QTDIR/examples/demo/widgets), which seems to work fine. I find this odd, to say the least.

Perhaps if I fiddle with it some more and take a closer look at the examples, I'll find out what the problem is. :)

Edit: Took a look at the qlayout header, and QGridLayout can take a layout as its parent afterall. Either that wasn't in the documentation or I missed it.

[ 04 April 2002: Message edited by: A. Buza ]

bwkaz
04-04-2002, 07:28 PM
Originally posted by A. Buza:
<STRONG>That's what I thought, too. I looked at the different constructors for QGridLayout, and there isn't one that takes a QLayout, only QWidgets. However, they pass a QLayout to the constructor in the example program I was looking at ($QTDIR/examples/demo/widgets), which seems to work fine. I find this odd, to say the least.</STRONG>

What they're probably doing there is giving it a parent layout that already exists. Something like, create a window with an HBox layout, and then in one of the cells in that layout, put a QGridLayout (with other widgets inside the QGridLayout). This way the grid takes one space in the HBox (or whatever they're using), but can subdivide its space between its child widgets.

The thing is, since you're using this QGridLayout to lay out the window, you can't use QGroupBox::layout() for that, because the QGroupBox doesn't have a valid layout yet. That layout is what you're creating!

I think if you replace ugBox-&gt;layout() with just ugbox, then it will work -- or at least, not segfault.

<STRONG>Edit: Took a look at the qlayout header, and QGridLayout can take a layout as its parent afterall. Either that wasn't in the documentation or I missed it.</STRONG>

No, it's there, at least for Qt 3, in Qt Assistant. Go to the QGridLayout class reference, and it's one of the constructors (the last one).

A. Buza
04-04-2002, 09:54 PM
That seems to have helped. It doesn't segfault anymore, and it almost displays the widgets properly. Right now the widgets inside the groupbox are drawn over part of the box. I think I can fix that pretty easily though.

Thanks for the help!