4
How can I resize the components inside the QWidget
when enlarging the window?
Example of how it looks:
4
How can I resize the components inside the QWidget
when enlarging the window?
Example of how it looks:
4
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.
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.
Take advantage and see also the documentation of QLayout
, in particular the QVBoxLayout
, QHBoxLayout
, QGridLayout
and QFormLayout
, covering the most common use cases.
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 c++ qt
You are not signed in. Login or sign up in order to post.
Are you creating the window and components via code or using the Qt window editor? You are setting a layout for the window?
– C. E. Gesser
Creating in the window editor. When it opens it becomes normal, but when it tries to enlarge, all objects stay fixed.
– Hy-brazil
Are you sure you set the layout of the window instead of just creating a layout inside the window?
– C. E. Gesser
Learn to use Layouts, watch this video: https://www.youtube.com/watch?v=E7Ud6FonsR4
– pepper_chico
Aee. was only selected the
Lay Out
. Thank you– Hy-brazil