====== GUI ====== ===== Расположение элементов ===== ----- **QBoxLayout**, **QVBoxLayout**, **QHBoxLayout**, **QGridLayout**. Вертикальная, горизонтальная, так же есть компоновка сеткой.\\
:!: Пример: использование горизонтальной и вертикальной компоновок {{:develop:qt:lay_hv.png?direct&400|}} /*...*/ labComboCapt= new QLabel("Выберите хост", this); comboHostServ= new QComboBox(this); comboHostServ->addItem(QHostAddress(QHostAddress::LocalHost).toString()); QHBoxLayout *layFirstRow= new QHBoxLayout(); layFirstRow->addWidget(labComboCapt); layFirstRow->addWidget(comboHostServ, 1); labEditCapt= new QLabel("Укажите порт", this); editPortServ= new QLineEdit(this); editPortServ->setText("2233"); QHBoxLayout *layDoubleRow= new QHBoxLayout(); layDoubleRow->addWidget(labEditCapt); layDoubleRow->addWidget(editPortServ, 1); labStatus= new QLabel("Здесь будет статус", this); butGetFortune= new QPushButton("Get Fort", this); butQuit= new QPushButton("Quit", this); QHBoxLayout *layButtsRow= new QHBoxLayout(); layButtsRow->addStretch(1); layButtsRow->addWidget(butGetFortune, 1); layButtsRow->addWidget(butQuit, 1); QVBoxLayout *layMain= new QVBoxLayout(this); layMain->addLayout(layFirstRow); layMain->addLayout(layDoubleRow); layMain->addWidget(labStatus, 0, Qt::AlignHCenter); layMain->addLayout(layButtsRow); /*...*/
:!: Пример: тоже самое только сеткой {{:develop:qt:lay_grid.png?direct&400|}}\\ * При добавлении указываются координаты ячейки и кол-во занимаемых ей строк/столбцов (-1 значит до конца) /*...*/ labComboCapt= new QLabel("Выберите хост", this); comboHostServ= new QComboBox(this); comboHostServ->addItem(QHostAddress(QHostAddress::LocalHost).toString()); labEditCapt= new QLabel("Укажите порт", this); editPortServ= new QLineEdit(this); editPortServ->setText("2233"); labStatus= new QLabel("Здесь будет статус", this); butGetFortune= new QPushButton("Get Fort", this); butQuit= new QPushButton("Quit", this); QGridLayout *layMainGrid= new QGridLayout(this); layMainGrid->addWidget(labComboCapt, 0, 0); layMainGrid->addWidget(comboHostServ, 0, 1, 1, -1); layMainGrid->addWidget(labEditCapt, 1, 0); layMainGrid->addWidget(editPortServ, 1, 1, 1, -1); layMainGrid->addWidget(labStatus, 2, 0, 1, -1, Qt::AlignCenter); layMainGrid->addWidget(butGetFortune, 3, 1); layMainGrid->addWidget(butQuit, 3, 2); layMainGrid->setColumnStretch(1, 1); layMainGrid->setColumnStretch(2, 1); /*...*/
В сетке **QLabel автоматом заполняет всю высоту**. И расстояние увеличивается пропорционально при увеличении размеров формы, а остальные строки "прижимаются" к краям.\\
:!: Пример: Непонятное поведение при заполнении сетки. {{:develop:qt:lay_def1.png?direct&400|}} {{:develop:qt:lay_def2.png?direct&400|}}\\ **В первом случае** однотипные строки\\ /*...*/ QGridLayout *layMainGrid= new QGridLayout(this); layMainGrid->setColumnStretch(1, 1); layMainGrid->setColumnStretch(2, 1); labComboCapt= new QLabel("Выберите хост", this); comboHostServ= new QComboBox(this); comboHostServ->addItem(QHostAddress(QHostAddress::LocalHost).toString()); layMainGrid->addWidget(labComboCapt, 0, 0); layMainGrid->addWidget(comboHostServ, 0, 1, 1, -1); labEditCapt= new QLabel("Укажите порт", this); editPortServ= new QLineEdit(this); editPortServ->setText("2233"); layMainGrid->addWidget(labEditCapt, 1, 0); layMainGrid->addWidget(editPortServ, 1, 1, 1, -1); QLabel *labEditCapt2= new QLabel("Укажите порт2", this); QLineEdit *editPortServ2= new QLineEdit(this); layMainGrid->addWidget(labEditCapt2, 2, 0); layMainGrid->addWidget(editPortServ2, 2, 1, 1, -1); butGetFortune= new QPushButton("Get Fort", this); butQuit= new QPushButton("Quit", this); layMainGrid->addWidget(butGetFortune, 4, 1); layMainGrid->addWidget(butQuit, 4, 2); /*...*/ **Во втором** добавлены лейблы внизу, они автоматом начинают занимать все свободное пространство\\ /*...*/ QGridLayout *layMainGrid= new QGridLayout(this); layMainGrid->setColumnStretch(1, 1); layMainGrid->setColumnStretch(2, 1); labComboCapt= new QLabel("Выберите хост", this); comboHostServ= new QComboBox(this); comboHostServ->addItem(QHostAddress(QHostAddress::LocalHost).toString()); layMainGrid->addWidget(labComboCapt, 0, 0); layMainGrid->addWidget(comboHostServ, 0, 1, 1, -1); labEditCapt= new QLabel("Укажите порт", this); editPortServ= new QLineEdit(this); editPortServ->setText("2233"); layMainGrid->addWidget(labEditCapt, 1, 0); layMainGrid->addWidget(editPortServ, 1, 1, 1, -1); labStatus= new QLabel("Здесь будет статус", this); layMainGrid->addWidget(labStatus, 2, 0); QLabel *labOver= new QLabel("This is overall label about suka layout"); layMainGrid->addWidget(labOver, 2, 1, 1, -1); butGetFortune= new QPushButton("Get Fort", this); butQuit= new QPushButton("Quit", this); layMainGrid->addWidget(butGetFortune, 4, 1); layMainGrid->addWidget(butQuit, 4, 2); /*...*/
===== ===== -----
:!: Пример: