Results 1 to 3 of 3

Thread: QHBoxLayout widget spacing

  1. #1
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default QHBoxLayout widget spacing

    Can't seem to get widgets in a layout spaced the way I want.
    Qt Code:
    1. // panel_1 class
    2. int w = 50;
    3. int h = 100;
    4.  
    5. pbStart = new QPushButton("Start");
    6. QHBoxLayout *layout_1 = new QHBoxLayout(this);
    7. layout_1->setSpacing(0);
    8.  
    9. layout_1->addWidget(pbStart);
    10.  
    11. lineEdit1 = new QLineEdit(this);
    12. lineEdit2 = new QLineEdit(this);
    13. lineEdit3 = new QLineEdit(this);
    14.  
    15. lineEdit1->setFixedSize(w, h);
    16. lineEdit2->setFixedSize(w, h);
    17. lineEdit3->setFixedSize(w, h);
    18.  
    19. layout_1->addWidget(lineEdit1);
    20. layout_1->addWidget(lineEdit2);
    21. layout_1->addWidget(lineEdit3);
    22.  
    23. // mainwindow class
    24. QWidget *widget = new QWidget(this);
    25. QVBoxLayout *layout = new QVBoxLayout;
    26. layout->setSpacing(1);
    27.  
    28. panel_1 = new Panel_1();
    29. layout->addWidget(panel_1);
    30. layout->addWidget(d_.view);
    31.  
    32. widget->setLayout(layout);
    33. setCentralWidget(widget);
    To copy to clipboard, switch view to plain text mode 
    The space between the pb and the line edits and between the edits is constant - about 2"
    Checking spacing() with debug, it reports 0.

    What am I doing wrong that's not letting me set the spacing to what I need?
    Thanks for any help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QHBoxLayout widget spacing

    I guess you hou have four fixed width widgets in a horizontal layout that is wider than the four widgets so the layout engine distributes the spare space evenly. Try adding the widgets to the layout with the alignment option of addWidget() specified.

  3. The following user says thank you to ChrisW67 for this useful post:

    K4ELO (7th March 2013)

  4. #3
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHBoxLayout widget spacing

    Thanks Chris, using the alignment option when adding the widget solves the problem.

Similar Threads

  1. Replies: 0
    Last Post: 12th July 2011, 09:19
  2. QHBoxLayout
    By VitaliBR in forum Qt Programming
    Replies: 7
    Last Post: 6th December 2010, 22:03
  3. How to remove Space Between 2 QHBoxLayout Widget
    By jaya in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2010, 23:57
  4. Replies: 0
    Last Post: 4th May 2010, 10:45
  5. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 08:57

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.