Results 1 to 6 of 6

Thread: QPushButtons not stretching in QGridLayout

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QPushButtons not stretching in QGridLayout

    I need the widgets in my gridlayout to be stretched so that it'll occupy the available space without any gaps. But I could'nt get that working.

    I have a QMainWindow, on which I have QStackedWidget as centralWidget. I have added a page (QWidget) to QStackedWidget and set QGridLayout to the page.
    Now I've added 3 QPushButtons in the top row of QGridLayout and 2 QPushButtons in the next row. I'm expecting the QPushButtons stretching across the layout both interms of horizontal & vertical spacing. Buthe buttons are of small size leaving large gaps in the layout. I tried the stretching, sizePolicy, but in vain. Please help me fix the issue.
    Thank you.

    Qt Code:
    1. lyt->addWidget(m_pbVulnerability, 0, 0, 2, 3, Qt::AlignCenter);
    2. lyt->addWidget(m_pbPatch, 0, 3, 2, 3, Qt::AlignCenter);
    3. lyt->addWidget(m_pbConfig, 0, 6, 2, 3, Qt::AlignCenter);
    4. lyt->addWidget(m_pbScan, 1, 3, 2, 6, Qt::AlignCenter);
    5. lyt->addWidget(m_scanTimeSelect, 5, 3, 2, 3, Qt::AlignCenter);
    6. lyt->addWidget(m_pbUpdate, 5, 6, 2, 3, Qt::AlignCenter);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: QPushButtons not stretching in QGridLayout

    Pushbutton->setMaximumSize() or maxHeight and maxWidth the thing u did wad make it so if its long then it will go into multiple grid spots but first u must make it long

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Re: QPushButtons not stretching in QGridLayout

    Hi giblit,

    I don't want to set any size to the buttons. As I'm using QGridLayout, I want to make use of layout properties for auto sizing of buttons. So any hint of where I'm doing wrong ?

    Qt Code:
    1. m_Button1 = new QPushButton("Button1", this);
    2. m_Button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    3.  
    4. m_Button2 = new QPushButton("Button2", this);
    5. m_Button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    6.  
    7. m_Button3 = new QPushButton("Button3", this);
    8. m_Button3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    9.  
    10. m_Button4 = new QPushButton("Button4");
    11. m_Button4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    12.  
    13. m_Button5 = new QPushButton("Update");
    14. m_Button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    15.  
    16. m_scanTimeSelect = new QSlider(Qt::Horizontal);
    17. m_scanTimeSelect->setTickInterval(12);
    18. m_scanTimeSelect->setTickPosition(QSlider::TicksBelow);
    19. m_scanTimeSelect->setStyleSheet("QSlider::groove:horizontal {border: 1px solid #bbb;background: white;height: 10px;border-radius: 4px;}"
    20. "QSlider::sub-page:horizontal {background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #66e, stop: 1 #bbf); background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1, stop: 0 #bbf, stop: 1 #55f); border: 1px solid #777; height: 10px; border-radius: 4px; }"
    21. "QSlider::add-page:horizontal { background: #fff; border: 1px solid #777; height: 10px; border-radius: 4px; }"
    22. "QSlider::handle:horizontal {background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #eee, stop:1 #ccc); border: 1px solid #777; width: 13px; margin-top: -2px; margin-bottom: -2px; border-radius: 4px; }"
    23. "QSlider::handle:horizontal:hover { background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #fff, stop:1 #ddd); border: 1px solid #444; border-radius: 4px; }"
    24. "QSlider::sub-page:horizontal:disabled { background: #bbb; border-color: #999; }"
    25. "QSlider::add-page:horizontal:disabled { background: #eee; border-color: #999; }"
    26. "QSlider::handle:horizontal:disabled { background: #eee; border: 1px solid #aaa; border-radius: 4px; }");
    27.  
    28. lyt->addWidget(m_Button1, 0, 0, 2, 3, Qt::AlignCenter);
    29. lyt->addWidget(m_Button2, 0, 3, 2, 3, Qt::AlignCenter);
    30. lyt->addWidget(m_Button3, 0, 6, 2, 3, Qt::AlignCenter);
    31. lyt->addWidget(m_Button4, 3, 3, 2, 6, Qt::AlignCenter);
    32. lyt->addWidget(m_scanTimeSelect, 5, 3, 2, 6, Qt::AlignCenter);
    33. lyt->addWidget(m_Button5, 5, 6, 2, 3, Qt::AlignCenter);
    To copy to clipboard, switch view to plain text mode 

    Kindly help me tackle this problem. Thank you.

  4. #4
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPushButtons not stretching in QGridLayout

    hi rawfool, add below lines i hope it shoud work.

    QPushButton *pButn = new QPushButton(this);

    QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(pButn->sizePolicy().hasHeightForWidth());
    pButn->setSizePolicy(sizePolicy);

    QPushButton *pButn1 = new QPushButton(this);
    pButn1->setSizePolicy(sizePolicy);

    QPushButton *pButn2 = new QPushButton(this);
    pButn2->setSizePolicy(sizePolicy);

    QPushButton *pButn3 = new QPushButton(this);
    pButn3->setSizePolicy(sizePolicy);

    lyt->addWidget(pButn, 0, 0, 2, 3);
    lyt->addWidget(pButn1, 0, 3, 2, 3);
    lyt->addWidget(pButn2, 0, 6, 2, 3);
    lyt->addWidget(pButn3, 3, 3, 2, 6);

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

    rawfool (15th April 2013)

  6. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPushButtons not stretching in QGridLayout

    Don't use Qt::AlignCenter while adding the widgets, this will force the layout to use the minimum/preferable size and pad the rest of area.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (15th April 2013)

  8. #6
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPushButtons not stretching in QGridLayout

    @ Pradeep - It worked! Thanks for the trick.
    @Santosh - Thanks for the insight.

Similar Threads

  1. QwtPlot stretching
    By jmsbc in forum Qwt
    Replies: 7
    Last Post: 27th August 2013, 07:43
  2. QGridLayout - Stop stretching
    By johnnyturbo3 in forum Newbie
    Replies: 1
    Last Post: 9th May 2011, 11:20
  3. No scrollbars or stretching in QToolBox
    By zenzero-2001 in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2010, 14:51
  4. Dockwidget stretching
    By jayreddy in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 15:37
  5. Stretching Icon in QPushButton
    By dsandber in forum Qt Tools
    Replies: 1
    Last Post: 6th October 2007, 16:51

Tags for this Thread

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.