Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Layout, widgets not inside ???
Forum Updated to NodeBB v4.3 + New Features

Layout, widgets not inside ???

Scheduled Pinned Locked Moved Unsolved General and Desktop
87 Posts 9 Posters 10.1k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jsulmJ jsulm

    @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?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #33

    @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.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #34

      @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.

      SPlattenS JoeCFDJ 2 Replies Last reply
      0
      • JoeCFDJ JoeCFD

        @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.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #35

        @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:
        Screenshot 2021-10-20 at 18.06.21.png
        What I don't understand is what is **[0] "verticalLayout" as a child ?

        Drill down a bit more into this layout:
        Screenshot 2021-10-20 at 18.25.22.png
        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".

        Kind Regards,
        Sy

        JoeCFDJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @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:
          Screenshot 2021-10-20 at 18.06.21.png
          What I don't understand is what is **[0] "verticalLayout" as a child ?

          Drill down a bit more into this layout:
          Screenshot 2021-10-20 at 18.25.22.png
          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".

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #36

          @SPlatten group box has a layout for three radio buttons and is a child as well. In total, it has 4 children.

          SPlattenS 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @SPlatten group box has a layout for three radio buttons and is a child as well. In total, it has 4 children.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #37

            @JoeCFD, please see edit, bit at the bottom.

            Kind Regards,
            Sy

            JoeCFDJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JoeCFD, please see edit, bit at the bottom.

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #38

              @SPlatten You created a verticalLayoutWidget inside a group box which has 4 children:
              verticalLayout
              radio1
              radio2
              radio3

              verticalLayoutWidget is not needed. the parent of verticalLayoutWidget is groupBox.

              SPlattenS 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @SPlatten You created a verticalLayoutWidget inside a group box which has 4 children:
                verticalLayout
                radio1
                radio2
                radio3

                verticalLayoutWidget is not needed. the parent of verticalLayoutWidget is groupBox.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #39

                @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.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @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.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #40

                  @JoeCFD
                  if there is no title for the group, set top margin to 0
                  QString group_style_sheet = QString("QGroupBox{border: 2px solid gray;border-radius: 8px;margin-top: 0px;}" );

                  SPlattenS 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @JoeCFD
                    if there is no title for the group, set top margin to 0
                    QString group_style_sheet = QString("QGroupBox{border: 2px solid gray;border-radius: 8px;margin-top: 0px;}" );

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #41

                    @JoeCFD , but why? What I'm trying to understand is why if done in Qt Creator WYSIWYG, no fiddles are required and it works, but the code generated from the XML just doesn't add up.

                    Kind Regards,
                    Sy

                    JoeCFDJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @JoeCFD , but why? What I'm trying to understand is why if done in Qt Creator WYSIWYG, no fiddles are required and it works, but the code generated from the XML just doesn't add up.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #42

                      @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.

                      SPlattenS 1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @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.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #43

                        @JoeCFD , please see latest edit....

                        Kind Regards,
                        Sy

                        JoeCFDJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @JoeCFD , please see latest edit....

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by JoeCFD
                          #44

                          @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 control

                          do this for each major layout.

                          SPlattenS 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @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 control

                            do this for each major layout.

                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by
                            #45

                            @JoeCFD , This was just an exercise using Qt Creator to see what it does and why the works compared to coding it by hand....

                            Kind Regards,
                            Sy

                            JoeCFDJ 1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @JoeCFD , This was just an exercise using Qt Creator to see what it does and why the works compared to coding it by hand....

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by JoeCFD
                              #46

                              @SPlatten
                              group layout

                              SPlattenS 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @SPlatten
                                group layout

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #47

                                @JoeCFD , can you not paste the source or post what you did?

                                Kind Regards,
                                Sy

                                1 Reply Last reply
                                0
                                • JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #48

                                  link added

                                  SPlattenS 1 Reply Last reply
                                  0
                                  • JoeCFDJ JoeCFD

                                    link added

                                    SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #49

                                    @JoeCFD , thank you, I'm still trying to see why I cannot replicate in code what the WYSIWYG editor does.

                                    Kind Regards,
                                    Sy

                                    1 Reply Last reply
                                    0
                                    • SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #50

                                      I thought I'd hit on something, I found another post online and added:

                                      pobjLayout->addItem(new QWidgetItem(pobjWidget));
                                      

                                      No joy, still doesn't work! Looking at the layout in the debugger, after executing this line, there are no additions to the children, same with addWidget...so what do I have to do?

                                      Kind Regards,
                                      Sy

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #51

                                        Provide a minimal compilable example of that specific part of your code so that people can experiment with it.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        SPlattenS 1 Reply Last reply
                                        2
                                        • SGaistS SGaist

                                          Provide a minimal compilable example of that specific part of your code so that people can experiment with it.

                                          SPlattenS Offline
                                          SPlattenS Offline
                                          SPlatten
                                          wrote on last edited by SPlatten
                                          #52

                                          @SGaist , this is difficult, but I will try:

                                          This is the XML:

                                          	<groupbox id="enSEX" eol="true" align="left" layout="vertical" title="Radios"
                                          			  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>
                                          

                                          I have quite a lot of code that parses all the various node types, here is the bit for the above:

                                                      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));
                                          

                                          The above code calls pCreate which based on the content of mstrName creates the widget:

                                          QWidget* clsCNT::pCreate(clsXMLnode* pobjNode, const QString& crstrType
                                                                  ,QString& rstrCSS, QStringList& rslstProperties
                                                                  ,QWidget* pobjParent) {
                                              if ( clsCNT::blnValidType(crstrType) == true ) {
                                                  if ( crstrType.compare(clsCNT::mscszButton) == 0 ) {
                                                      return new clsQtPushBtn(pobjNode, &rstrCSS, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszCheckbox) == 0 ) {
                                                      return new clsQtCheckBox(pobjNode, &rstrCSS, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszCombo) == 0 ) {
                                                      return new clsQtComboBox(pobjNode, &rstrCSS, &rslstProperties, pobjParent);
                                                  } else if( crstrType.compare(clsCNT::mscszFrame) == 0 ) {
                                                      return new clsQtFrame(pobjNode, &rstrCSS, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszGroupBox) == 0 ) {
                                                      return new clsQtGroupBox(pobjNode, &rstrCSS, &rslstProperties, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszLabel) == 0 ) {
                                                      return new clsQtLabel(pobjNode, &rstrCSS, &rslstProperties, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszLineEdit) == 0 ) {
                                                      return new clsQtLineEdit(pobjNode, &rstrCSS, &rslstProperties, pobjParent);            
                                                  } else if ( crstrType.compare(clsCNT::mscszListWidget) == 0 ) {
                                                      return new clsQtListWidget(pobjNode, &rstrCSS, &rslstProperties, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszRadioButton) == 0 ) {
                                                      return new clsQtRadioButton(pobjNode, &rstrCSS, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszSliderVert) == 0 ) {
                                                      return new clsQtSlider(Qt::Vertical, pobjNode, &rstrCSS, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszSliderHorz) == 0 ) {
                                                      return new clsQtSlider(Qt::Horizontal, pobjNode, &rstrCSS, pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszSpinBox) == 0 ) {
                                                      return new QSpinBox(pobjParent);
                                                  } else if ( crstrType.compare(clsCNT::mscszTextEdit) == 0 ) {
                                                      return new clsQtTextEdit(pobjNode, &rstrCSS, &rslstProperties, pobjParent);
                                                  }
                                              }
                                              return nullptr;
                                          }
                                          

                                          Give the widget a human readable name and add it to the layout if one exists:

                                                  if ( pobjWidget != nullptr ) {
                                          //Set the object name
                                                      QString strID(strGetAttribute(clsXMLnode::mscszAttrID)),
                                                              strName(mstrName);
                                                      if ( strID.isEmpty() != true ) {
                                                          strName += ", id: " + strID;
                                                      }
                                                      pobjWidget->setObjectName(strName);
                                                  }
                                                  if ( pobjLayout != nullptr ) {
                                          //Any alignment?
                                                      QString strAlign(pobjParent->strGetAttribute(clsXMLnode::mscszAttrAlignment));
                                                      Qt::Alignment eCellAlign(Qt::AlignVCenter | Qt::AlignHCenter);
                                                      if ( strAlign.compare(clsXMLnode::mscszGeomCenter) == 0 ) {
                                                          eCellAlign |= Qt::AlignHCenter;
                                                      } else if ( strAlign.compare(clsXMLnode::mscszGeomLeft) == 0 ) {
                                                          eCellAlign |= Qt::AlignLeft;
                                                      } else if ( strAlign.compare(clsXMLnode::mscszGeomRight) == 0 ) {
                                                          eCellAlign |= Qt::AlignRight;
                                                      }
                                          //Add the widget to the layout
                                                      QLayoutItem* pobjItem(new QWidgetItem(pobjWidget));
                                                      pobjItem->setAlignment(eCellAlign);
                                                      pobjLayout->addItem(pobjItem);
                                          //            pobjLayout->addWidget(pobjWidget);
                                                  }
                                          

                                          Hopefully this is enough, however at this point I really don't think its something I've done wrong but a limitation or restriction in Qt, bearing in mind that my code does this all at run-time where as the UI create in Qt is parsed when the project is compiled.

                                          [Edit], I just decided to debug again and single stepped into addItem, interesting:

                                          void QBoxLayout::addItem(QLayoutItem *item)
                                          {
                                              Q_D(QBoxLayout);
                                              QBoxLayoutItem *it = new QBoxLayoutItem(item);
                                              d->list.append(it);
                                              invalidate();
                                          }
                                          

                                          When I get into addItem, on QBoxLayoutItem *it = new QBoxLayoutItem(item);:
                                          Screenshot 2021-10-20 at 20.25.33.png

                                          If I click on Step Over twice, the cursor is pointing to invalidate();, the watch window has changed to:
                                          Screenshot 2021-10-20 at 20.27.08.png

                                          Looks very wrong and after returning back to where the call took place there is no addition to the layout.

                                          addWidget calls addItem internally, so if its a bug in addItem this explains everything.

                                          [Edit 2] I have file a bug:
                                          https://bugreports.qt.io/browse/QTBUG-97662

                                          Kind Regards,
                                          Sy

                                          JKSHJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved