[Solved] Dynamic Layout should be better then that what I've done
-
Hi there,
I'm very new to QT (using 5.4). First, I get trough the zetcode Tutorial (for QT 4) and now I try to develop my very first own Application with QT.
My problem: to get the UI Dynamic Layout in a nice behaviour (I mean a behaviour where it looks good when its small and when its big), I think I've used more lines then necessary.
My Code:
mainwindow.cpp
@#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
/VBox deklarieren - nur einmal QLayout definieren/
QVBoxLayout *vbox = new QVBoxLayout(this);
vbox->setMargin(20);
/HBoxes deklarieren/
QHBoxLayout *hbox_1 = new QHBoxLayout();
QHBoxLayout *hbox_2 = new QHBoxLayout();
QHBoxLayout *hbox_3 = new QHBoxLayout();/*Strings definieren*/ QLabel *l_title = new QLabel("Titel", this); QLabel *l_subtitle = new QLabel("Subtitel", this); /*Strings formatieren*/ l_title->setAlignment(Qt::AlignCenter); l_title->setFont(QFont("Calibri", 36, QFont::Bold)); l_title->setStyleSheet("QLabel {color: green}"); l_subtitle->setAlignment(Qt::AlignCenter); /*Buttons definieren*/ QPushButton *b_1 = new QPushButton("Button 1 ", this); b_1->setMaximumWidth(200); QPushButton *b_2 = new QPushButton("Button 2", this); b_2->setMaximumWidth(200); QPushButton *b_3 = new QPushButton("Button 3", this); b_3 ->setMaximumWidth(200); /*Layout definieren*/ /*HBox1*/ vbox->addStretch(1); hbox_1->addWidget(b_1, 0, Qt::AlignVCenter); vbox->addStretch(1); /*HBox2*/ vbox->addStretch(1); hbox_2->addWidget(b_2, 0, Qt::AlignVCenter); vbox->addStretch(1); /*HBox3*/ vbox->addStretch(1); hbox_3->addWidget(b_3, 0, Qt::AlignVCenter); vbox->addStretch(1); /*VBox*/ vbox->addStretch(1); vbox->addWidget(l_title, 0, Qt::AlignHCenter); vbox->addStretch(1); vbox->addWidget(l_subtitle, 0, Qt::AlignHCenter); vbox->addStretch(5); vbox->addLayout(hbox_1); vbox->addStretch(1); vbox->addLayout(hbox_2); vbox->addStretch(1); vbox->addLayout(hbox_3); vbox->addStretch(10); vbox->addSpacing(50); /*Layout setzen*/ setLayout(vbox);
}@
It would be nice If someone can give me some hints.
greetings
-casisto -
Sure? I had asked for help, not for a jester...
-
Hi and welcome to devnet,
Spam removed
Your code doesn't look bad, you could use a loop to create and setup your three buttons since you do three times the exact the same thing
-
Hi sGaist,
thanks, and thanks for removing the Spam.
I've now edited my Layout with a loop. But I get the error:
"QLayout::addChildLayout: layout "" already has a parent"The arrays a declared in mainwindow.h and lang.h:
QPushButton *btn[2];
QHBoxLayout *hbox[2];
QString str_btn[] = {"btn1", "btn2", "btn3"};@#include "mainwindow.h"
#include "lang.h"MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
/VBox deklarieren - nur einmal QLayout definieren/
QVBoxLayout *vbox = new QVBoxLayout(this);
vbox->setMargin(20);/*Strings definieren*/ QLabel *l_title = new QLabel("Titel", this); QLabel *l_subtitle = new QLabel("Subtitel", this); /*Strings formatieren*/ l_title->setAlignment(Qt::AlignCenter); l_title->setFont(QFont("Calibri", 36, QFont::Bold)); l_title->setStyleSheet("QLabel {color: green}"); l_subtitle->setAlignment(Qt::AlignCenter); for(int i = 0; i < 3; i++) { /*Buttons definieren*/ btn[i] = new QPushButton(str_btn[i], this); /*Hboxes definieren*/ hbox[i] = new QHBoxLayout(); /*HBoxLayout definieren*/ hbox[i]->addStretch(1); hbox[i]->addWidget(btn[i], 0, Qt::AlignVCenter); hbox[i]->addStretch(1); } vbox->addStretch(1); vbox->addWidget(l_title, 0, Qt::AlignHCenter); vbox->addStretch(1); vbox->addWidget(l_subtitle, 0, Qt::AlignHCenter); vbox->addStretch(5); vbox->addLayout(hbox[0]); vbox->addStretch(1); vbox->addLayout(hbox[1]); vbox->addStretch(1); vbox->addLayout(hbox[2]); vbox->addStretch(1); /*Layout setzen*/ setLayout(vbox);
}
@It would be great if you could give me hint what throw the warning? (Also, I get the first button in the upper left corner, but I think the first error conditioned the second...)
-
I've missed that one so:
@QVBoxLayout *vbox = new QVBoxLayout(this);@
Here you already set vbox as layout of your widget so the call to
@setLayout(vbox);@
is causing the warning.
-
Ok,
now I've get it.
Funny thing, I had change the first line to this:
@ QVBoxLayout *vbox = new QVBoxLayout;@Comment out the
@setLayout(vbox);@has finished in an "unexpected close".
Thanks, Samuel