How to relocate and resize the widgets in the mainwindow
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?
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.
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?
void MainWindow::ui_Implementation(Ui::MainWindow *ui)
ui->label->setText("Camera serial number:");
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;
QGroupBox *grpBox = new QGroupBox;
QVBoxLayout *mainLayout = new QVBoxLayout;
QWidget *cWidget = new QWidget(this);
Check if this fulfill your requirements.
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?
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.
@QVBoxLayout *mainLayout = new QVBoxLayout;
You can attach an image of ur requirement .
Reimplement "resize event":http://doc.qt.nokia.com/latest/qwidget.html#resizeEvent.