PDA

View Full Version : Coding Precise Layouts



JimDaniel
15th September 2007, 20:53
Hi, I'm writing some code that generates menus (QWidget) with buttons (QPushButtons) and labels (QLabel) from an XML file. Each menu has a QGridLayout and nested within those I have various other layouts of buttons and labels.

I'm wondering what techniques are used to to get the most precise control over the layout. Everything so far is very imprecise and I can't seem to find the right methods to control the layout the way I want.

If anyone could tell me the best ways to control layout through code, or point me to a tutorial for this, I would be very grateful...

fullmetalcoder
15th September 2007, 21:03
Yours explanation are not very precise but I'm sure QWidget::setSizePolicy() would help a lot (if not solve everything...), especially combined with proper mergins and spacing of the layouts (QLayout::setSpacing() and QLayout::setMargin()).

jpn
15th September 2007, 21:04
I'd like to point out that Qt Designer already creates XML files (.ui), which QUiLoader is able to load at runtime. :)

marcel
15th September 2007, 21:41
I'm wondering what techniques are used to to get the most precise control over the layout. Everything so far is very imprecise and I can't seem to find the right methods to control the layout the way I want.

What is the way you want to control it?

JimDaniel
15th September 2007, 22:12
What is the way you want to control it?

I thought maybe if you could see my problem it would make it easier to help. This is my current menu. The multi-colored buttons are in a QGridLayout which I have laid-out okay. The three labels and small blue button are in a QHBoxlayout. Both of these layouts are nested in the menu's QGridLayout at row 1/column 1 and row 2/column 1. I realize at the moment a grid layout might not be necessary, but we plan to add other layouts to this screen as well.

I would like for the labels to be closer set together and aligned on the top-left side of the screen, not spread out over the width of the entire screen. The small blue button needs to be on the top-right. And the multi-colored button grid needs to be aligned in the center - right now it acts like the horizontal label layout is pushing it down.

http://i209.photobucket.com/albums/bb277/bresson29/examplelayout-1.jpg

If you can give me any pointers I would be grateful. I'm starting to play around with the methods which fullmetalcoder has suggested, but haven't gotten anywhere just yet...

marcel
15th September 2007, 22:23
I would like for the labels to be closer set together and aligned on the top-left side of the screen, not spread out over the width of the entire screen. The small blue button needs to be on the top-right.

Add a QSpacerItem(QLayout::addItem()) between the labels and the blue button. This will make the labels stay at the left side and the button stick to the right side.
If you don't want the left margin of the labels to be aligned with the left margin of the button grid, then take the horizontal layout outside the grid layout and set it's margin and spacing to 0. Also, now you will have to add the horizontal layout and the grid layout to a parent layout to hold them together, preferably a vertical layout.

Generally, it is better to have a simple top layout and add all the components to this layout. Further, the components should be split also in basic parts. This makes it simple to create and manage layouts.



And the multi-colored button grid needs to be aligned in the center - right now it acts like the horizontal label layout is pushing it down

Not sure about that.

JimDaniel
15th September 2007, 22:30
Thanks for the tips! I'll keep working at it...