How to relocate and resize the widgets in the mainwindow
-
Dear All,
I am new to QT programming and how can I resize and relocate the widgets like buttons and labels proportionally to the main window's size? How can I implement it? If possible, can I have a sample program?
Thanks,
Thomas -
There is Layout system in Qt for such kind of things. Take a look here: "Layout Examples":http://qt-project.org/doc/qt-4.8/examples-layouts.html
-
You can also check these "video tutorials":http://www.voidrealms.com/tutorials.aspx?filter=qt It's a good start for beginners.
-
Hi,
What I did is I have labels in the main window.
They are put into the vertical layout.
Then the vertical layout is put inside the group box.
I like to make when the main window size is changed, all sizes are changed proportionally. The code is as follow.
But when I run, even though my window size is changed, the widget sizes are not changed, also their positions. Where could be the error?
Thanks
void MainWindow::ui_Implementation(Ui::MainWindow *ui)
{
ui->label->setText("Camera serial number:");
ui->label_3->setText("Camera model:");
ui->label_5->setText("Interface:");
ui->label_7->setText("Image size:");QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(ui->groupBox); mainLayout->addLayout(ui->verticalLayout); setLayout(mainLayout);
}
-
Since you are using a QMainWindow you need to add a QWidget and set it as centralWidget eg
@QLabel *labelOne = new QLabel("Camera serial number:");
QLabel *labelTwo = new QLabel("Camera model:");
QLabel *labelThree = new QLabel("Interface:");
QLabel *labelFour = new QLabel("Image Size:");QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(labelOne);
vLayout->addWidget(labelTwo);
vLayout->addWidget(labelThree);
vLayout->addWidget(labelFour);QGroupBox *grpBox = new QGroupBox;
grpBox->setLayout(vLayout);QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(grpBox);QWidget *cWidget = new QWidget(this);
cWidget->setLayout(mainLayout);setCentralWidget(cWidget);
@Check if this fulfill your requirements.
-
Hi Sam,
Thanks for the sample.
I did that but the problem is all label are equally spaced and take the whole area of the main window.
Actually I want to have fixed at a position and grow/shrink according to the main window size.
Where can I change that?
Thanks,
Thomas -
So which of the following you need
- Grow/Shrink Horizontally.
- Grow/Shrink Vertically
- Both (currently used)
Anyhow you can add a stretch to the layout. Since it is a VBoxLayout, if u adld a stretch the labels will grow/shrink horizontally.
eg:
@QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(grpBox);
mainLayout->addStretch();
@You can attach an image of ur requirement .
-
Reimplement "resize event":http://doc.qt.nokia.com/latest/qwidget.html#resizeEvent.