Resize QWidget within QBoxLayout
-
Hi. I am placing a QPushButton within a QBoxLayout. This is my code.
MessageBoxCenter::MessageBoxCenter(QWidget *parent): QMessageBox(parent), ui(new Ui::MessageBoxCenter) { ui->setupUi(this); //Build a QDialog Box. dialogBox = new QDialog(this); dialogBox->setWindowTitle("Not enough inputs!"); //Set size of QDialog. dialogBox->resize(500, 400); QVBoxLayout *boxLayout = new QVBoxLayout(dialogBox); dialogBox->setLayout(boxLayout); //Add a QLabel, unused atm boxText = new QLabel(this); //Add a QButton boxButton = new QPushButton("reee", this); //boxButton->resize(200, 80); //THIS IS THE IMPORTANT BIT boxButton->setMinimumSize(200, 80); //THIS IS THE IMPORTANT BIT boxLayout->addWidget(boxButton, 0, Qt::AlignCenter); qDebug() <<"setup ButtonSize " << boxButton->size(); connect(boxButton, SIGNAL(clicked()), dialogBox, SLOT(close())); }
According to the basic layouts example, "Each widget will get at least its minimum size and at most its maximum size." However, for my current code, my widgets are entirely squished to their minimum size. I would like to know whether there is a function that declares within QBoxLayout that it should take the preferred size of widgets instead of their minimum. So far I have been resizing by resetting the minimum size but I would like to use the proper resize funciton.
Here is some other code I tried based on another part of QBoxLayout documentation, "If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved." However, the button remains at it's minimum size.
ui->setupUi(this); //Build a QDialog Box. dialogBox = new QDialog(this); dialogBox->setWindowTitle("Not enough inputs!"); //Set size of QDialog. dialogBox->resize(500, 400); //450, 200 QVBoxLayout *boxLayout = new QVBoxLayout(dialogBox); dialogBox->setLayout(boxLayout); //Add a QLabel boxText = new QLabel(this); boxButton = new QPushButton("reee", this); boxButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); //My understanding is that the QBoxLayout will be resized based on the QSizePolicy of the QWidget. boxButton->resize(200, 80); boxLayout->addWidget(boxButton, 0, Qt::AlignCenter); qDebug() <<"setup ButtonSize " << boxButton->size(); connect(boxButton, SIGNAL(clicked()), dialogBox, SLOT(close()));
Please let me know if more information is required.