Layout, widgets not inside ???
-
@SPlatten said in Layout, widgets not inside ???:
if the parent of the radio buttons has a layout then they are added to the layout
That may be the problem. What if the parent hasn’t got a layout (yet)? Does your code throw an exception, returns an error or just stops? Or does it place the buttons outside the group box? I tend to believe that your code provokes the behaviour since I have never seen a bug in Qt that fiddles with widget assignments.
-
@AxelVienna , I manage everything, only the nodes I add have widgets and only the widgets I assign a layout have a layout… unless QT is doing something I’m not aware of.
-
A bold statement…. A lot of volunteers have read through your posts, tried to help and advise. If there are reasons to maintain your approach and you can’t find the bug, you have to hire an expert.
-
@SPlatten said in Layout, widgets not inside ???:
I just can't see how my code differs from the example.
Parent handling from child widgets, that's the main difference I already suggested to avoid.
-
@SPlatten said in Layout, widgets not inside ???:
QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons")); QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1")); QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2")); QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3")); radio1->setChecked(true); QVBoxLayout *vbox = new QVBoxLayout; vbox->addWidget(radio1); vbox->addWidget(radio2); vbox->addWidget(radio3); vbox->addStretch(1); groupBox->setLayout(vbox); return groupBox;
auto groupBox = new QGroupBox(tr("Exclusive Radio Buttons"), this ); auto radio1 = new QRadioButton(tr("&Radio button 1"), groupBox); auto radio2 = new QRadioButton(tr("R&adio button 2"), groupBox); auto radio3 = new QRadioButton(tr("Ra&dio button 3"), groupBox); auto button_group = new QButtonGroup; button_group->addButton(radio1) ; button_group->addButton(radio2) ; button_group->addButton(radio3) ; button_group->setExclusive( true ); radio1->setChecked(true); auto vbox = new QVBoxLayout( groupBox ); vbox->addWidget(radio1); vbox->addWidget(radio2); vbox->addWidget(radio3); vbox->addStretch(1); return groupBox;
-
QString group_style_sheet = QString("QGroupBox{border: %1px solid gray;border-radius: %2px;margin-top: %3px;}" ) .arg( m_groupBorderThickness ) .arg( m_groupBorderRadius ) .arg( text_font_size * 1.6 );
Also you may need to set group style sheet to move text to the top of group frame. I do use it. Otherwise, the title text may push the radio buttons out of the group frame. Set minimum height to your group box.
If you do not want to set border properties, it is OK. But it is better to set margin top which is related to your group font size. Play with font size and parameter 1.6 and you will be able to move the radio buttons into the group frame. -
@JoeCFD, I'm not sure what you are demonstrating here? The section of code I pasted comes directly from the Qt link, also posted.
What purpose does the QButtonGroup serve?
I edited my XML and applied the style:
<groupbox id="enSEX" eol="true" align="left" layout="vertical" dbfield="vcSex" properties="QGroupBox { border-radius: 8px; background-color:#ff0000; };"> <radiobutton id="rdoM" text="Male" default="true" position="0,0"/> <radiobutton id="rdoF" text="Female" position="1,0"/> </groupbox>
Here is the result:
-
@SPlatten said in Layout, widgets not inside ???:
QButtonGroup
QButtonGroup serves making radio buttons exclusive. It is a common use. Otherwise, you need to handle them. Works for other buttons as well.
Adding stylesheet with margin-top will help you move the radio buttons into the group box(set some minimum size to the groupbox to accomodate these buttons). I had the same problem as yours before. I solved the issue with stylesheet. Did you notice your title text("Exclusive Radio Buttons") does not show because it is inside the group box. The first radio button is there too. -
@SPlatten It is true that that is a bug in the example. You copied it from there. If qbuttongroup is not used, you have to have a slot onButtonClicked to toggle them(messy). Sometimes, you may need it for your app.
QButtonGroup is very helpful. Now you know how to use it.
-
@JoeCFD , I'll try to describe what I'm working on, I am developing an engine that reads the configuration of what will be contained in a window from an XML set of files. The XML can be edited and reloaded into the engine with no source code changes required, the engine renders the content of the XML file converting it to widgets, as an example:
<?xml version="1.0" encoding="utf-8"?> <form title="Test form" id="frmTest" hspacing="0" vspacing="0" width="480" height="480" modal="false"> <entry control="listwidget" label="Select:" id="enSEL" cols="24" rows="1" mode="single" excludeFromChangeCheck="true" resetPropertiesOnChange="true" eol="true"> <subscriber signal="itemSelectionChanged" target="simon2.js@listWidgetHandler"/> <subscriber signal="currentItemChanged" target="simon2.js@listWidgetChanged"/> </entry> <entry control="lineedit" label="First name:" id="enFN" eol="true" cols="24" maxlength="24" accept="alpha_only" dbfield="vcFirstName"/> <entry control="lineedit" label="Surnname:" id="enSN" eol="true" cols="24" maxlength="24" accept="alpha_only" dbfield="vcSurName"/> <groupbox id="enSEX" eol="true" align="left" layout="vertical" dbfield="vcSex" properties="QGroupBox { border-radius: 8px; background-color:#ff0000; }"> <radiobutton id="rdoM" text="Male" default="true" position="0,0"/> <radiobutton id="rdoF" text="Female" position="1,0"/> </groupbox> <entry control="checkbox" label="Employed:" id="enEmp" eol="true" dbfield="intEmployed"/> <entry control="textedit" label="Address:" id="enADR" eol="true" maxlength="256" accept="alpha_numeric" cols="24" rows="5" dbfield="vcAddress"/> <groupbox id="enButtons" eol="true" align="right" layout="horizontal"> <button id="btnApply" api="applyChanges"> <subscriber signal="clicked" target="simon2.js@applyButton"/> </button> <button id="btnUndo" api="undoChanges"/> <button id="btnOK" api="submitAndClose"/> </groupbox> <on saveChanges="Save changes before changing selection?"/> <on setup="simon2.js@setupForm"/> </form>
I'm still having problems in getting the layout and groupbox to work properly, now I am getting a problem where after setting the layout, the code jumps into QWidgetRepaintManager::removeDirtyWidget.
This happens immediately after I call setLayout on the QGroupBox widget.
-
Are you giving a parent to the layout before calling setLayout ?
-
@SPlatten Good stuff. Can you please show your code for building layout? XML file has only properties of widgets and can not tell why layout goes wrong.
A good practice for debugging is to keep a small GUI case with ui file generated from qt designer and then you compile this case. From ui_*** file created in build, you can see how widgets are laid out. In this way, you can save a lot of time. I believe your time worths some good money.
-
@JoeCFD , the issue I have now is that I've created a mock up using Qt Creator and in this I added a QGroupBox, then into this I added a QVBoxLayout then added three QRadioButtons, I then examined the document as XML and everything looked fine, when I build and ran it also looked fine.
However I'm having difficulty replicating this, I have a QGroupBox, I've created a layout and used setLayout on the QGroupBox widget, so far so good....but now I want to set the parent to the layout but can't, because the layouts are not derived from QWidget and calling ->widget() on the layout returns nullptr. I want to use the layout like a widget so I can add other widgets to it using the layout as a parent.
-
@SPlatten said in Layout, widgets not inside ???:
I want to use the layout like a widget so I can add other widgets to it using the layout as a parent.
You can't. A layout is not QWidget. And I'm wondering why you want to do this? There is already a simple way to add a widget to a layout.
-
@jsulm, I've tried implementing this:
QLayout* pobjLayout(nullptr); if ( pobjParent != nullptr ) { //Does the parent have a layout? pobjLayout = pobjParent->pobjGetLayout(); if ( pobjLayout != nullptr ) { //Yes, clear the pobjParWidget widget pobjParWidget = nullptr; } } pobjWidget = qobject_cast<QWidget*>(clsCNT::pCreate(this, mstrName, strCSS, slstProperties, pobjParWidget)); if ( pobjLayout != nullptr ) { //Add the widget to the layout pobjLayout->addWidget(pobjWidget); }
However this doesn't work either, the radio buttons still appear outside of the groupbox and not in the layout, I've single stepped the logic, so the radio button widgets are added to the layout.
-
@SPlatten said in Layout, widgets not inside ???:
pobjLayout->addWidget(pobjWidget)
Is this line executed?
If it is: to which widget does this layout belong? What is pobjGetLayout() actually doing? -
@jsulm , yes, it is. I've used setObjectName to assign the layouts and widgets the id's they have in the XML.
In the above pobjLayout points to a QVBoxLayout and pobjWidget points to a QRadioButton.
The layout is appended to a QGroupBox via setLayout.
-
@JoeCFD said in Layout, widgets not inside ???:
QString group_style_sheet = QString("QGroupBox{border: %1px solid gray;border-radius: %2px;margin-top: %3px;}" )
.arg( m_groupBorderThickness )
.arg( m_groupBorderRadius )
.arg( text_font_size * 1.6 );override the style sheet of group box in the xml with
int margin_top = font().pointSize()* 1.6;
QString group_style_sheet = QString("QGroupBox{border: 2px solid gray;border-radius: 8px;margin-top: %1px;}" ).arg( margin_top );
You try to hardcode it and see if it works.