QHboxLayout , how to put buttons and checkbox side by side



  • I have a requirement in which I have to place the buttons and check boxes side by side .For that I am using following code

    /* creating the refresh button/checkbox at the bottom widget*/
    QWidget* bottomWidget = new QWidget;
    QHBoxLayout layout = new IN_CURRENT_POOL QHBoxLayout;
    bottomWidget->setLayout(layout);
    QPushButton
    button1 = new IN_CURRENT_POOL QPushButton("Button1");
    QCheckBox* checkBox1 = new IN_CURRENT_POOL QCheckBox("CheckBox");
    QCheckBox *checkBox1 = new IN_CURRENT_POOL QCheckBox("CheckBox2");

    button1->setMaximumWidth(80);
    

    checkBox1->setMaximumWidth(120);
    controlsLayout->addWidget(button1);
    controlsLayout->addWidget(checkBox1);
    controlsLayout->addWidget(checkBox2);

    To place it side by side I am using following code as shown above

    button1->setMaximumWidth(80);
    checkBox1->setMaximumWidth(120);

    Is ther any other better way to achieve the requirement "I have to place the buttons and check boxes side"


  • Moderators

    @Qt-Enthusiast said:

    QPushButton button1 = new IN_CURRENT_POOL QPushButton("Button1");

    This is not a pointer! This should give already a compile error.



  • Sorry it was a typo I mean
    QPushButton* button1 = new IN_CURRENT_POOL QPushButton("Button1");

    If you could answer my question


  • Qt Champions 2016

    @Qt-Enthusiast
    If I understand correctly what you want to achieve, I'd use a horizontal spacer to eat up the space instead of setting the maximum size for the elements. Something along the lines of:

    controlsLayout->addWidget(button1);
    controlsLayout->addWidget(checkBox1);
    controlsLayout->addWidget(checkBox2);
    controlsLayout->addStretch(1);
    

  • Moderators

    @Qt-Enthusiast

    Sorry I thought you had an error when compiling.

    I do not see a better solution.

    Without going into detail it looks pretty similar to the documentation.. Another possibly to check things is also to use designer and check the files generated by meta compiler.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.