Results 1 to 8 of 8

Thread: Make QVBoxLayout act like QFormLayout

  1. #1
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Make QVBoxLayout act like QFormLayout

    I've been trying to make a QVBoxLayout act like a QFormLayout so that when I add widgets to the QVBoxLayout, they do not stretch across the length of the parent widget. I've been trying to do this with sizePolicies, sretchFactors, etc, but I can't seem to figure it out.

    Here's my code:
    Qt Code:
    1. QPushButton* thing1 = new QPushButton(QObject::tr("Hi"));
    2. QPushButton* thing2 = new QPushButton(QObject::tr("There"));
    3. QPushButton* thing3 = new QPushButton(QObject::tr("Again"));
    4.  
    5. QVBoxLayout* layout = new QVBoxLayout();
    6. layout->addWidget(thing1);
    7. layout->addWidget(thing2);
    8. layout->addWidget(thing3);
    9.  
    10. QWidget* widget = new QWidget();
    11. widget->resize(300,300);
    12. widget->setLayout(layout);
    13. widget->show();
    To copy to clipboard, switch view to plain text mode 

    Output using a QFormLayout:



    Output using a QVBoxLayout:



    I'd like the QVBoxLayout version to mimic the QFormLayout version.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Make QVBoxLayout act like QFormLayout

    Why don't you just use QFormLayout then?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Make QVBoxLayout act like QFormLayout

    I suppose I could in some cases, but what if I wanted to use a QHBoxLayout instead of a QVBoxLayout (to my [limited] knowledge there is no way to make a sideways QFormLayout). I'm trying to understand what property of layouts lets me modify the layout's behavior when i expand the window.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Make QVBoxLayout act like QFormLayout

    Maybe I'm missing something but won't QVBoxLayout::addStretch() do what you want?

  5. The following user says thank you to norobro for this useful post:

    petwoip (8th March 2011)

  6. #5
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Make QVBoxLayout act like QFormLayout

    Oh cool, addStretch(0) achieves the desired behavior!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Make QVBoxLayout act like QFormLayout

    Quote Originally Posted by petwoip View Post
    I suppose I could in some cases, but what if I wanted to use a QHBoxLayout instead of a QVBoxLayout (to my [limited] knowledge there is no way to make a sideways QFormLayout). I'm trying to understand what property of layouts lets me modify the layout's behavior when i expand the window.
    The layout behaviour is decided upon settings of the widgets (their size hint and size policies). Your problem is not a problem with QHBoxLayout as it will do what you want out of the box.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Make QVBoxLayout act like QFormLayout

    setAlignment(Qt::AlignTop); should also do the trick

  9. #8
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Make QVBoxLayout act like QFormLayout

    Thanks for the help guys! This is a lot clearer to me now.

Similar Threads

  1. Alternate row colours in QFormLayout
    By xtal256 in forum Qt Programming
    Replies: 9
    Last Post: 27th February 2011, 22:36
  2. remove row of QFormLayout
    By jorg in forum Newbie
    Replies: 1
    Last Post: 31st January 2011, 05:19
  3. Removing Rows from QFormLayout
    By jeffbobble in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2010, 19:54
  4. How to collapse rows of a QFormLayout?
    By abey in forum Qt Programming
    Replies: 2
    Last Post: 14th December 2009, 07:39
  5. qformlayout: no addSpacing?
    By mattc in forum Qt Programming
    Replies: 0
    Last Post: 11th October 2009, 19:30

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.