Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Resize QWidget within QBoxLayout
Forum Updated to NodeBB v4.3 + New Features

Resize QWidget within QBoxLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 184 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved