Resize components when enlarging window

Asked

Viewed 1,144 times

4

How can I resize the components inside the QWidget when enlarging the window?

Example of how it looks:

exemplo

  • Are you creating the window and components via code or using the Qt window editor? You are setting a layout for the window?

  • Creating in the window editor. When it opens it becomes normal, but when it tries to enlarge, all objects stay fixed.

  • Are you sure you set the layout of the window instead of just creating a layout inside the window?

  • Learn to use Layouts, watch this video: https://www.youtube.com/watch?v=E7Ud6FonsR4

  • Aee. was only selected the Lay Out. Thank you

1 answer

4


Solution: layouts

You can use layouts instead of placing components directly in the widget at pre-determined positions by fixed coordinates.

Its interface becomes much more consistent, becoming independent of the font sizes or visual style of the system in question, in addition to providing a better use of the screen when the UI is well built.

Using Qt Designer

If you are using Qt Designer just put the controls in the approximate position in the desired widget, select the widget and apply a layout, on the buttons highlighted below, or insert a layout directly into the widget by taking from the list on the left, and then add controls in the desired places.

Captura de tela do Qt Designer com ítens destacados

Take advantage and see also the documentation of QLayout, in particular the QVBoxLayout, QHBoxLayout, QGridLayout and QFormLayout, covering the most common use cases.

Using layouts directly in source code

Exemplo de layouts aninhados

Source code:

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog();

private:
    void createMenu();
    void createHorizontalGroupBox();
    void createGridGroupBox();
    void createFormGroupBox();

    enum { NumGridRows = 3, NumButtons = 4 };

    QMenuBar *menuBar;
    QGroupBox *horizontalGroupBox;
    QGroupBox *gridGroupBox;
    QGroupBox *formGroupBox;
    QTextEdit *smallEditor;
    QTextEdit *bigEditor;
    QLabel *labels[NumGridRows];
    QLineEdit *lineEdits[NumGridRows];
    QPushButton *buttons[NumButtons];
    QDialogButtonBox *buttonBox;

    QMenu *fileMenu;
    QAction *exitAction;
};

Dialog::Dialog()
{
    createMenu();
    createHorizontalGroupBox();
    createGridGroupBox();
    createFormGroupBox();

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);

    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->setMenuBar(menuBar);

    mainLayout->addWidget(horizontalGroupBox);
    mainLayout->addWidget(gridGroupBox);
    mainLayout->addWidget(formGroupBox);
    mainLayout->addWidget(bigEditor);
    mainLayout->addWidget(buttonBox);

    setLayout(mainLayout);

    setWindowTitle(tr("Basic Layouts"));
}

void Dialog::createGridGroupBox()
{
    gridGroupBox = new QGroupBox(tr("Grid layout"));

    for (int i = 0; i < NumGridRows; ++i) {
        labels[i] = new QLabel(tr("Line %1:").arg(i + 1));
        lineEdits[i] = new QLineEdit;
        layout->addWidget(labels[i], i + 1, 0);
        layout->addWidget(lineEdits[i], i + 1, 1);
    }

    smallEditor = new QTextEdit;
    smallEditor->setPlainText(tr("This widget takes up about two thirds of the "
                                "grid layout."));
    layout->addWidget(smallEditor, 0, 2, 4, 1);

    layout->setColumnStretch(1, 10);
    layout->setColumnStretch(2, 20);
    gridGroupBox->setLayout(layout);
}

void Dialog::createFormGroupBox()
{
    formGroupBox = new QGroupBox(tr("Form layout"));
    QFormLayout *layout = new QFormLayout;
    layout->addRow(new QLabel(tr("Line 1:")), new QLineEdit);
    layout->addRow(new QLabel(tr("Line 2, long text:")), new QComboBox);
    layout->addRow(new QLabel(tr("Line 3:")), new QSpinBox);
    formGroupBox->setLayout(layout);
}

See this code with more explanations on Qt documentation

Remember that both in Qt Designer and via source code, you can nest layouts as in the example above, to have greater control over the interface.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.