View Full Version : Layout Managers???

17th February 2006, 03:56
Hello again,

Does ANYBODY know anything about layout managers??? I haven't gotten any information here and there's nothing in the documentation or Qt book (using 3.3.4).

In the docs it shows us card.cpp and card.h which is their own layout manager. I implemented it and it crashes. Great. However, it doesn't crash anywhere in visible code, but it's definitely the manager that's crashing. I don't even know if I'm calling it right because there's no mention of how to even use it.

How do I add a widget??? Widgets don't inherit qlayout and layouts don't inherit qwidget, so how is this thing supposed to work?

HomeLayout->addItem( (QWidget *) qLineEdit1 ); or

HomeLayout->addItem( (QWidgetItem *) qLineEdit1 );

Please help, as this is NOT ( :mad: )in the documentation and I don't have a clue how it's supposed to work.:confused: NOTHING Works With This!


17th February 2006, 04:33
You don't use the QLayout directly (You can't even, as it's an abstract base class). Use a QBoxLayout or a QGridLayout.

Both have addWidget() and addLayout() methods.


17th February 2006, 07:35
I'm just implementing the custom layout manager as in this example.


Unfortunately I haven't been able to get it to run without crashing. There's nothing in this example which states how to set it up and nothing I've tried works. I think it has something to do with QLayoutItem vs. QWidgetItem and such, but who know. Do you know how to use this custom example?

Has anyone, ANYONE gotten this example to work?


17th February 2006, 08:18
You can't run the example itself? Or code you've tried to write based off of it?

The example compiles and runs for me without complaint (qt 3.3.4, gcc 4.0.2)

If it's your code, can you show more of what you're trying to do?

17th February 2006, 18:10
Their code compiles without problems, but it continually crashes, usually within the bowels of the minimumSizeHint or sizeHint, but it's impossible to find (using vc++). I haven't modified it at all. The only code of mine that's relevant I posted above where I call it using a QLineEdit, or rather 2 of them. I renamed CardLayout to HomeLayout, and that's the only modification I made to their code.

frButtons is just a frame that contains the 2 qlineedits.

HomeLayout1 = new HomeLayout((QWidget *)frButtons, 6);
leA = new QLineEdit( frButtons, "lineEditA" );
leB = new QLineEdit( frButtons, "lineEditB" );

HomeLayout1->addItem( leA );
HomeLayout1->addItem( leB );
This doesn't compile, I get error: error C2664: 'HomeLayout::addItem' : cannot convert parameter 1 from 'QLineEdit *' to 'QLayoutItem *'

So, I tried casting leA and leB to ('QLayoutItem *) in the call to addItem which does compile, and then crashes on running, somewhere within minimumSizeHint or sizeHint (access violation).

How do you call addItem? Can you show me what you did for the testing code?:o

Thank you for the help,

17th February 2006, 18:16
So the code crashes not because layouts are bad, but because you cast a pointer of one class to another class, which it doesn't inherit, which is a "no no".


QLayoutItem *lit = new QWidgetItem(le1);

At least types will match :)

17th February 2006, 21:18
Yeah, I tried that too. I'm out of ideas of what to try.

I'd love to get some very simple test code in which this layout works with a few widgets. Any help there:o

Thank you!


18th February 2006, 04:42
What if you use the add() method, rather than the addItem() method to add your widgets?

That's what the example uses. It's defined in QLayout, and takes a QWidget as a parameter.