Layout, widgets not inside ???
-
@SPlatten
:)And I see there is a bigger example at https://doc.qt.io/qt-5/qtwidgets-widgets-groupbox-example.html, but just the standalone one should do.
On a separate note, and not wanting to distract you from the task at hand. You seem to have said that you can get what you want from Designer? But not from your own code. And you're not sure what Designer way has done it? But when you build you run
uic
on the.ui
file and (IIRC) it generatesui_yourfile.h
. In that issetupUi()
. And that is all the code implementing whatever you have designed, there isn't anything else to it. So that code must show you how they produce what you say works? -
@JonB , I've used Qt Creator to create a mock layout, adding a Group Box, 3 x Radio Buttons then selected Lay Out Vertically from the Lay Out context submenu. Closed UI, clicked on Run qmake then opened up ui_clsMainWnd.h and searched for groupBox, this I the code:
groupBox = new QGroupBox(centralWidget); groupBox->setObjectName(QString::fromUtf8("groupBox")); groupBox->setGeometry(QRect(340, 210, 127, 101)); verticalLayout = new QVBoxLayout(groupBox); verticalLayout->setSpacing(6); verticalLayout->setContentsMargins(11, 11, 11, 11); verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); radioButton = new QRadioButton(groupBox); radioButton->setObjectName(QString::fromUtf8("radioButton")); verticalLayout->addWidget(radioButton); radioButton_2 = new QRadioButton(groupBox); radioButton_2->setObjectName(QString::fromUtf8("radioButton_2")); verticalLayout->addWidget(radioButton_2); radioButton_3 = new QRadioButton(groupBox); radioButton_3->setObjectName(QString::fromUtf8("radioButton_3")); verticalLayout->addWidget(radioButton_3);
This all looks good, I assume that 6 used or spacing is just a default along with the margins 11. I may have to experiment with the geometry 340, 210, 127, 101.
-
@SPlatten
Not sure what the question is here. Does "This all looks good" mean that when run it does indeed draw the radiobuttons correctly within their layout, as you have been trying to do in code but unsuccessfully?You can see the code is exactly what I said and in the code example in the docs for
QGroupBox
. Create aQGroupBox
, put aQVBoxLayout
on it, addQRadioButtons
onto the layout. That is all you should ever do.The
setGeometry()
just corresponds to the position & size of the groupbox you have on your design canvas. I would not have thought you would need to do that, theQGroupBox
should resize to correctly enclose its content. The spacing and margins are just defaults used by Designer, you can alter them.You can play with this code in the
ui_clsMainWnd.h
file by editing, it will not get overwritten unless you alter the.ui
file. And/or you can copy it to your own code and play with it there.Whatever the difference from your own code which has not been working, you should be in a good position to discover that from here.
-
@SPlatten
Indeed, you must just make sure that what you come up from your parsing-creating-dynamically matches the static pattern you know works.Everything has always felt like the radiobuttons are being added onto the
QGroupBox
directly where they need to be added onto theQVBoxLayout
(and of course that must be set as the group box's layout). -
@JonB , been out all day, just got back to trying out the stand along demo and no problems, it works:
#include "mainwindow.h" #include <QApplication> #include <QGroupBox> #include <QRadioButton> #include <QVBoxLayout> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QGroupBox* groupBox = new QGroupBox(w.centralWidget()); groupBox->setTitle("Demo"); groupBox->setObjectName(QString::fromUtf8("groupBox")); groupBox->setGeometry(QRect(20, 110, 127, 101)); QVBoxLayout* verticalLayout = new QVBoxLayout(groupBox); verticalLayout->setSpacing(6); verticalLayout->setContentsMargins(11, 11, 11, 11); verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); QRadioButton* radioButton = new QRadioButton(groupBox); radioButton->setObjectName(QString::fromUtf8("radioButton")); radioButton->setText("Radio 1"); verticalLayout->addWidget(radioButton); QRadioButton* radioButton_2 = new QRadioButton(groupBox); radioButton_2->setObjectName(QString::fromUtf8("radioButton_2")); radioButton_2->setText("Radio 2"); verticalLayout->addWidget(radioButton_2); QRadioButton* radioButton_3 = new QRadioButton(groupBox); radioButton_3->setObjectName(QString::fromUtf8("radioButton_3")); radioButton_3->setText("Radio 3"); verticalLayout->addWidget(radioButton_3); w.show(); return a.exec(); }
Which means I need to did around in my engine and find out why the same doesn't work when I repeat in my engine.
Debugger, watch window:
-
As suggested before, you should simplify your code and approach the interface building in a simpler manner.
For your xml, you should pass it through a validator to ensure that what you parse is what your application expects. With that you can remove quite a lot of manual verifications.
Split your widget generation in smaller functions. It's harder to reason in one mega function that does everything.