Layout, widgets not inside ???
-
@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. -
@JoeCFD , thank you appreciated, why isn't this kind of thing required when positioning and using Qt Creator? This is the XML copied from the UI file which works perfectly.
<widget class="QGroupBox" name="groupBox"> <property name="geometry"> <rect> <x>120</x> <y>170</y> <width>301</width> <height>201</height> </rect> </property> <property name="title"> <string>GroupBox</string> </property> <widget class="QWidget" name="verticalLayoutWidget"> <property name="geometry"> <rect> <x>9</x> <y>29</y> <width>291</width> <height>161</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QRadioButton" name="Radio1"> <property name="text"> <string>Radio1</string> </property> </widget> </item> <item> <widget class="QRadioButton" name="Radio2"> <property name="text"> <string>Radio2</string> </property> </widget> </item> <item> <widget class="QRadioButton" name="Radio3"> <property name="text"> <string>Radio3</string> </property> </widget> </item> </layout> </widget> </widget> </widget> </widget>
No sign of any styles or positioning, I'm now trying to reverse the logic to see where the difference is.
This is what I found in the debugger looking at how the above XML appears:
What I don't understand is what is **[0] "verticalLayout" as a child ?Drill down a bit more into this layout:
It appears that the groupBox has one child which I agree with and expect verticalLayoutWidget, the layout has 4 children which I didn't expect:- verticalLayout
- Radio1
- Radio2
- Radio3
What is verticalLayout and where did it come from, it has no children, it isn't in the XML layout ?
Just to clarify, just in case the screenshot isn't clear it shows:
groupBox verticalLayout verticalLayoutWidget Radio1 Radio2 Radio3
Its verticalLayoutWidget that is the mystery...
Another edit, looked again at the XML and it does match the debugger screenshot. It looks like there is a fiddle in that the first widget is not the QVBoxLayout but a Widget called "verticalLayoutWidget".
-
@JoeCFD , to repeat what I did:
- Dragged Group Box from Containers palette and dropped onto centralWidget
- Dragged Vertical Layout from Layouts palette and dropped onto Group Box
- Dragged Radio Button from Buttons palette and dropped onto Vertical Layout
- Dragged Radio Button from Buttons palette and dropped onto Vertical Layout
- Dragged Radio Button from Buttons palette and dropped onto Vertical Layout
- Edited text attribute for each control
Thats it, one layout not two. Take a look at the XML I posted, its pretty clear and its the actual elements that are generated from this XML that I'm questioning.
-
@SPlatten Could be some global settings in your project which cause this problem. Since all my widgets are customized, I do not complain about this. BTW, border-radius setting is bad sometimes when screen resolution is not high enough. Often, I have to set it to 0.
-
@SPlatten
Dragged Group Box from Containers palette and dropped onto centralWidget
Dragged Radio Button from Buttons palette
Dragged Radio Button from Buttons palette
Dragged Radio Button from Buttons palette
right click inside qgroupbox and select layout->Lay out Vertically ---> size policy is added here automatically.
Edited text attribute for each controldo this for each major layout.