[solved] placing QPushButtons inside a QVbox on mainwindow
-
I want to place some QPushButtons inside a QVboxLayout from within code
Mainwindow was created as mainwindow.ui using QT-Creator Designer.
Following code creating a centerd pushbutton with full windowsize width regardless
any geometry settings.
I think i need to set "centralwidget" layout (?) but can not figure out how to do this.@void MainWindow::on_pb_test_2_clicked()
{
QVBoxLayout *vb = new QVBoxLayout(MainWindow::centralWidget());
vb->setObjectName(QString("vb_1"));
vb->setGeometry(QRect(100, 100,25,100));QPushButton *pb_1 = new QPushButton; pb_1->setObjectName(QString("pb_1")); pb_1->setStyleSheet(QString("QPushButton {background-color: red;}")); pb_1->setGeometry(QRect(0, 0, 15, 15)); pb_1->setText("o"); vb->addWidget(pb_1);
// ? ui->centralWidget
// ? MainWindow->setCentralWidget(centralWidget());
}@This code creates only a single pushbutton for now to keep things more clear.
I found some 'solutions' on this forum but any of them fixes the problem for me.
Thanks in advance. -
You probably need to set the layout of the central widget:
@
ui->centralWidget()->setLayout(<your Vbox>);
@ -
thanks for reply, but no change, still a full window width pushbutton, vertically centered on mainwindow.
Built without any error.I added to position line 13 in above code
@ ui->centralWidget->setLayout(vb);@
When i do the design i want using the designer i see in
ui_mainwindow.h this lines:@verticalLayoutWidget = new QWidget(centralWidget);
verticalLayoutWidget->setObjectName(QString::fromUtf8("verticalLayoutWidget"));
verticalLayoutWidget->setGeometry(QRect(60, 70, 21, 261));verticalLayout = new QVBoxLayout(verticalLayoutWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);@in the "manual" code i do not use anything similar to:
@verticalLayoutWidget = new QWidget(centralWidget);@maybe this is my mistake ?
-
Argh, I misread. So you have a pushbutton that hold the entire screen????
That is what it suppose to do. Use the spacers to indicate to the layout that 'empty' space should be created if the pushbutton is beyond it's maximum size or geometry settings. Otherwise the Layout will force the pushbutton to take the entire screen. -
so i need yo add "spacers" in the manual code ?
puhI just want the pushbutton stays inside the given geometry
of the vboxlayout. I fear i need to read a lot more to get this work. -
Got it!
tomorrow i 'll paste it here. Now i need a drink :) -
Just QVBoxLayout is not sufficient, i need a QWidget too.
(i guess the QVBoxLayout is an abstract object only
and needs a widget object to 'materialise' )
Reduced to the essentials the following code works:
@void MainWindow::on_pb_test_clicked()
{
QWidget *vLW = new QWidget(MainWindow::centralWidget());
vLW->setObjectName(QString::fromUtf8("vLW"));
vLW->setGeometry(QRect(150, 100, 31, 250));QVBoxLayout *vL = new QVBoxLayout(vLW); vL->setObjectName(QString::fromUtf8("vL")); QPushButton *pushButton = new QPushButton(vLW); pushButton->setObjectName(QString::fromUtf8("pushButton")); pushButton->setMaximumSize(QSize(20, 20)); pushButton->setStyleSheet(QString("QPushButton {background-color: red;}")); vL->addWidget(pushButton); vLW->show();
}@