Results 1 to 7 of 7

Thread: Coding Precise Layouts

  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Coding Precise Layouts

    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...

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Precise Layouts

    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()).
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Coding Precise Layouts

    I'd like to point out that Qt Designer already creates XML files (.ui), which QUiLoader is able to load at runtime.
    J-P Nurmi

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Precise Layouts

    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?

  5. #5
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Coding Precise Layouts

    Quote Originally Posted by marcel View Post
    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.



    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...

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Precise Layouts

    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.

  7. #7
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Coding Precise Layouts

    Thanks for the tips! I'll keep working at it...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.