PDA

View Full Version : why QT layout doesn't work on the second widget?



Kevin Hoang
15th March 2010, 08:59
I have a GUI Application, the main form is widget and display as full screen. The main widget contains some sub-widgets and controls, they was placed in layouts. To this point, my application is perfect fluid and work well.

Now, I add a second widget with a class, create few layouts, and add buttons and sub-widgets into layouts as same way I do with main widget. In the a button on main widget, I call to this second widget, the second widget show, but I don't know why the layouts doesn't works.

Somebody help me why the layout doesn't work on the second widget? Thanks!

ChrisW67
15th March 2010, 09:20
Have you set a layout on the second widget as a whole?

Can you post a picture of the second widget when "the layouts doesn't work" and explain what it is the isn't working? Can you post the ui file?

Kevin Hoang
15th March 2010, 15:05
Have you set a layout on the second widget as a whole?

Can you post a picture of the second widget when "the layouts doesn't work" and explain what it is the isn't working? Can you post the ui file?

Yes, I have. I don't know why it doesn't works right now.

I have attach my example project. help me!

ChrisW67
16th March 2010, 05:45
Neither of the two form UI files has a layout set on the top-level. In Designer select the Form object in the Object browser. Notice the symbol with a crossed red circle? That means no layout is applied. Right click the Form object entry, select "Lay out", then select the layout of your choice. If the widgets on the form need nested layout then you add them in a similar way.

If you remedy this then the code you have placed in show() of both form classes is not needed (and probably should have been in the constructor anyway). You can call QWidget::showFullScreen() instead of show() from main() too.

Kevin Hoang
19th March 2010, 06:15
Thank Chris!

I follow your instruction. I created layouts by Qt Designer, but nothing change, I still having the same problem.

I don't want to setup layouts on Qt Designer, because it makes me very hard to order or add the controls.

ChrisW67
19th March 2010, 07:48
I don't understand your aversion to using Designer to set a layout manager. You can easily remove widgets from a layout and re-add them elsewhere, even when Designer is generating the initial code for you. But hey, different strokes for different folks.

Anyway, in the Form class move all the code in the show() method into the constructor after setupUi(). Do the same for the Widget class but delete the line calling showFullScreen(). Delete the show() method from both classes. In main.cpp call w.showFullScreen().

How is that?

Kevin Hoang
19th March 2010, 09:14
Thank Chris very much!

I don't understand events on QT, why it doesn't works when I place it in show event.
I move that code to constructor, if I add in to that code a message box, the layouts doesn’t works. Is that mean the layouts have to setup first?

ChrisW67
19th March 2010, 23:27
The show "event" (and its sibling hide()) is a method to make a constructed widget object visible (not visible). Imagine you have a dialog on which your compound widget is only visible when another setting is true, do you really want to create a new set of layout objects every time the widget is shown? I suspect not. The construction (the bulk of it) need only happen once and the logical place is the constructor, that's why Qt Creator's templates put the Designer Ui setup there.

When you say "I don't want to setup layouts on Qt Designer, because it makes me very hard to order or add the controls." Can you explain what you want to do that you feel Designer is stopping you doing?

Kevin Hoang
20th March 2010, 10:50
I know the construction happen once, but we can't call it again, if I want to change the layout when users resize the form to make form always look beautiful, the problem starting here. Such as, in case, I have to build a layout has spaces, and changes the order display of controls. How to update the layout in this case?

Maybe, I don’t have experiences to use QT Designer. I drag a layout to form, really hard to add a control to this layout as directly. It made some different between design and result. I still having change on code to make it look same design.

Thank you for your discussion.

ChrisW67
20th March 2010, 22:29
You could see how close you can get using a QGridLayout, which allows widgets to span grid cells,control sizing with stretch factors, and dynamically add/remove and rearrange them. If your idea of "beautiful" can be implemented by this layout then you could change it as needed in the QWidget::resizeEvent ( QResizeEvent * event ) (leave the layout manager in place, just remove the widgets and reinsert them in the right places). You might chose between one of two widget arrangements based on the ratio of width to height (portrait vs. landscape) and let the default layout handle it.

For something truly fluid, with nothing in lines etc. you could implement your own layout manager by deriving from QLayout, apply that to your widget, and let it do its thing.

You don't say what the application is. I haven't done anything for a mobile phone. There may well be better solutions for space management of a small display.