Results 1 to 7 of 7

Thread: How can I programmatically add QPushButtons in a QFrame without using Layout?

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question How can I programmatically add QPushButtons in a QFrame without using Layout?

    Hi,

    I add qpushbuttons on Form and set my buttons' positions with setGeometry. It's ok.
    Now I want to divide these buttons into QFrame as the groups.

    I add buttons on Form as follow:
    myBtn[btn]=new QPushButton(this);
    myBtn[btn]->setGeometry(intWidth*i,y,intWidth,intWidth);

    How can I add these buttons into QFrame? VBoxLayout, HBoxLayout or GridLayout is not suitable for my buttons' position.
    So I don't want to use layout.

    Thanks.

  2. #2
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I programmatically add QPushButtons in a QFrame without using Layout?

    Make your push buttons child of QFrame.

    Qt Code:
    1. QFrame frame = new QFrame(this);
    2. myBtn[btn]=new QPushButton(frame);
    3. myBtn[btn]->setGeometry(intWidth*i,y,intWidth,intWidth);
    To copy to clipboard, switch view to plain text mode 

    Now setGeometry will set the push button positions with respect to its parent QFrame.

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

    binary001 (15th December 2015)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How can I programmatically add QPushButtons in a QFrame without using Layout?

    Not using a layout is usually a bad idea. Are you having such a dynamic situation that you need to move the buttons a lot?

    In any case, I don't quite understand your problem.
    Layouts don't add widgets to frames, they manage the size and location within the parent widget, whether that is a frame or some other widget.

    Cheers,
    _

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

    binary001 (15th December 2015)

  6. #4
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How can I programmatically add QPushButtons in a QFrame without using Layout?

    Thanks for your quick replay.

    Now I save my button locations and sizes in struct array with map number.
    I place freely these buttons with maps without any layout.
    QFrame frame = new QFrame(this);
    myBtn[btn]=new QPushButton(frame);
    myBtn[btn]->setGeometry(intWidth*i,y,intWidth,intWidth);
    It's OK.

    Thanks anda_skoa for your suggestion.
    I also think "Not using a layout is usually a bad idea". But I can't decide which type of layout should I use to add there buttons on the Form according to my maps?

    map1.png

    Please see picture of my button position and size. It's a map number 1.
    There are many maps to place buttons. Number of buttons and locations are different for maps.
    Thanks

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 748 Times in 738 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How can I programmatically add QPushButtons in a QFrame without using Layout?

    Please see picture of my button position and size. It's a map number 1.
    There are many maps to place buttons. Number of buttons and locations are different for maps.
    What is a "map"? Is it a button or is it a container (widget) where you want to place one or more buttons?

  8. #6
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How can I programmatically add QPushButtons in a QFrame without using Layout?

    I want to add 7 buttons (please see above my post). The buttons' positions and sizes are shown at picture ( I named a group of buttons' sizes and positions as a map).
    Which layout can I use to place buttons on a Form?

    Thanks.

  9. #7
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I programmatically add QPushButtons in a QFrame without using Layout?

    You should be using a QGridLayout.
    Especially rowSpan and columnSpan with the alignment should be sufficient to get the widgets as shown in your image.

    Qt Code:
    1. void QGridLayout::addWidget(QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Vikram.Saralaya for this useful post:

    binary001 (16th December 2015)

Similar Threads

  1. QPushButtons not stretching in QGridLayout
    By rawfool in forum Newbie
    Replies: 5
    Last Post: 15th April 2013, 14:14
  2. Control spacing of a QFrame layout using a Stylesheet
    By gmat4321 in forum Qt Programming
    Replies: 1
    Last Post: 21st October 2012, 19:18
  3. How to fill a QFrame space in QSplitter layout
    By henryjoye in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2010, 05:18
  4. Where is my QPushButtons
    By HelloDan in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2009, 07:15
  5. Replies: 1
    Last Post: 1st November 2007, 07:25

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.