Results 1 to 4 of 4

Thread: setAlignment in Layout

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default setAlignment in Layout

    I have a QVBoxLayout. I've set the alignment like so:

    Qt Code:
    1. myVBLay->setAlignment(Qt::AlignRight);
    To copy to clipboard, switch view to plain text mode 

    If I add buttons or labels to the layout, shouldn't they Align to the Right? They don't: always left, no matter what I try. What could I possibly be missing? (I've tried setting the alignment right on the QLabel, too. Nothing.).
    Last edited by vonCZ; 28th April 2009 at 18:11.

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

    Default Re: setAlignment in Layout

    Yes, they should. Without seeing the code it is hard to say what you did wrong. Please provide a minimal compilable example reproducing the problem.
    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
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setAlignment in Layout

    The snippet below illustrates the problem. I have a QVBoxLayout for which I've set the Alignment to AlignCenter. I've then added two labels to it. The first label is properly centered, but only because it is within it's own HBoxLayout, for which AlignCenter is set.

    Qt Code:
    1. // Vertical Box Layout
    2. QVBoxLayout *wlogVB_cLL = new QVBoxLayout;
    3. wlogVB_cLL->setAlignment(Qt::AlignCenter);
    4.  
    5. // FIRST item
    6. QHBoxLayout *wlogHB_dLL = new QHBoxLayout;
    7. wlogHB_dLL->setAlignment(Qt::AlignCenter);
    8. QLabel *laWellLL = new QLabel;
    9. laWellLL->setText("First");
    10. wlogHB_dLL->addWidget(laWellLL);
    11. wlogVB_cLL->addLayout(wlogHB_dLL);
    12.  
    13. // SECOND item
    14. QLabel *laWellLL2 = new QLabel;
    15. laWellLL2->setText("Second");
    16. wlogVB_cLL->addWidget(laWellLL2);
    17.  
    18. // Additional items, some of which are in HBoxLayouts
    To copy to clipboard, switch view to plain text mode 

    The second label, however: it is Aligned to the left... despite the fact that I set the *vertical* box layout to be AlignCenter. The only way I can get it to align center: put it in an HBoxLayout, just like "first".

    Perhaps this is the way it's supposed to work... though I don't understand why.

    My guess (I can't test it at the moment): if the VBoxLayout *only* had widgets, the setAlignment(Qt::AlignCenter) would work. But because it contains a mix of widgets and layouts, it doesn't work... and therefore *all* of the widgets must be contained in an HBoxLayout, each of which has alignment set to Center.
    Last edited by vonCZ; 29th April 2009 at 07:32.

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

    Default Re: setAlignment in Layout

    Everything is ok. QLayout::setAlignment(Qt::Alignment) changes the alignment of the layout, not the items inside it. You should use QLayout::setAlignment(QWidget *, Qt::Alignment) variant instead.
    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.


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

    Cupidvogel (17th January 2016)

Similar Threads

  1. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  2. Qt like Layout Manager available for .NET platform
    By vkhaitan in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2008, 13:36
  3. Qt layout memory issue
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 25th August 2007, 17:11
  4. Resizing problems when applying a layout
    By JimBrown in forum Newbie
    Replies: 1
    Last Post: 21st February 2007, 22:54
  5. "dynamic" layout
    By hulk in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2006, 07:16

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.