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 scratching head...
Forum Updated to NodeBB v4.3 + New Features

Layout scratching head...

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 2 Posters 2.2k Views 2 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.
  • M mchinand

    @SPlatten said in Layout scratching head...:

    The group box has the horizontal layout as its parent:

    This part doesn't sound correct. The horizontal layout you add the buttons to shouldn't be the parent of the QGroupBox. Here's the QGroupBox example code. There should be a separate layout for widgets within the QGroupBox.

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

    @mchinand , did you see the [edit] section at the end of my post? that works but I can't see whats different about what I am doing.

    In the code that works I set the layout for the group then add the group the the vertical layout.

    In the code that doesn't work, same, I set the layout for the group and is a child of the layout.

    I'm looking closer at the code that works and in this, none of the widgets have parent nodes, could this explain why it works and the code that doesn't work, all nodes have parents?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #4

      I think it might just look like it's working because the QGroupBox is added to a QVBoxLayout which is the same type of layout the buttons are added to (when it 'works'). Is your QGroupBox visible in your screenshot? For testing purposes, can you set the title to your QGroupBox?

      SPlattenS 1 Reply Last reply
      0
      • M mchinand

        I think it might just look like it's working because the QGroupBox is added to a QVBoxLayout which is the same type of layout the buttons are added to (when it 'works'). Is your QGroupBox visible in your screenshot? For testing purposes, can you set the title to your QGroupBox?

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

        @mchinand , yes, thats the small grey box above the buttons, I think because I it has no content, its too small to show a Title, I just tried and the title isn't displayed.

        Kind Regards,
        Sy

        M 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @mchinand , yes, thats the small grey box above the buttons, I think because I it has no content, its too small to show a Title, I just tried and the title isn't displayed.

          M Offline
          M Offline
          mchinand
          wrote on last edited by
          #6

          Oh, OK, I understand now. I thought that screenshot was showing when it was working with a QVBoxLayout for the buttons, but it isn't working even with that. The buttons aren't inside the QGroupBox.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mchinand
            wrote on last edited by
            #7

            You need to create a new layout for the items you want in your QGroupBox.

            SPlattenS 1 Reply Last reply
            0
            • M mchinand

              You need to create a new layout for the items you want in your QGroupBox.

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

              @mchinand said in Layout scratching head...:

              You need to create a new layout for the items you want in your QGroupBox.

              If you take at look at the XML in my post, thats exactly what is done:

              	<layout type="horizontal" width="128">
              		<groupbox id="btnbar"/>
              		<button id="btnApply" group="btnbar" api="applyChanges">
              			<subscriber signal="clicked" target="simon2.js@applyButton"/>
              		</button>
              		<button id="btnUndo" group="btnbar" api="undoChanges"/>
              		<button id="btnOK" group="btnbar" api="submitAndClose"/>	
              	</layout>		
              

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mchinand
                wrote on last edited by
                #9

                @SPlatten said in Layout scratching head...:

                If you take at look at the XML in my post, thats exactly what is done:

                Your QGroupBox shouldn't be part of this layout and the layout should not be the QGroupBox's parent. The QGroupBox's parent will automatically become whatever layout it is added to.

                SPlattenS 1 Reply Last reply
                0
                • M mchinand

                  @SPlatten said in Layout scratching head...:

                  If you take at look at the XML in my post, thats exactly what is done:

                  Your QGroupBox shouldn't be part of this layout and the layout should not be the QGroupBox's parent. The QGroupBox's parent will automatically become whatever layout it is added to.

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

                  @mchinand , ok, I've removed the parent from the QGroupBox, unfortunately same result, no difference.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #11

                    @SPlatten said in Layout scratching head...:

                       QLayout* pobjLayout(pobjParent->layout());
                    

                    How are you getting the layout for the buttons now? It should be a new layout, so I don't think you can obtain it with this line.

                    SPlattenS 1 Reply Last reply
                    0
                    • M mchinand

                      @SPlatten said in Layout scratching head...:

                         QLayout* pobjLayout(pobjParent->layout());
                      

                      How are you getting the layout for the buttons now? It should be a new layout, so I don't think you can obtain it with this line.

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

                      @mchinand , as the XML is processed, the layout is created, the first item added to the layout is the group box, then the buttons.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mchinand
                        wrote on last edited by
                        #13

                        @SPlatten said in Layout scratching head...:

                        the first item added to the layout is the group box

                        Do not do this step.

                        SPlattenS 1 Reply Last reply
                        0
                        • M mchinand

                          @SPlatten said in Layout scratching head...:

                          the first item added to the layout is the group box

                          Do not do this step.

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

                          @mchinand , in my sample code that works:

                              QHBoxLayout* phbxLayout(new QHBoxLayout);
                              QGroupBox* pobjGroup(new QGroupBox);
                              pobjGroup->setLayout(phbxLayout);
                              for( int i=1; i<=3; i++ ) {
                                  QPushButton* pbtnTemp(new QPushButton(QString("Button %1").arg(i)));
                                  phbxLayout->addWidget(pbtnTemp);
                              }
                              pvbxLayout->addWidget(pobjGroup);
                          

                          In the code that doesn't work:

                                  QWidget* pobjWidget(mpobjWidget);
                                  if ( pobjWidget != nullptr ) {
                                      QLayout* pobjLayout(pobjWidget->layout());
                                      if ( pobjLayout == nullptr && mpobjParent != nullptr ) {
                              //Does the parent have a layout?
                                          if ( mpobjParent->mpobjWidget != nullptr ) {
                                              pobjLayout = mpobjParent->mpobjWidget->layout();
                                          }
                                      }
                                      if ( pobjLayout != nullptr ) {
                                          QFormLayout* pfrmlo(qobject_cast<QFormLayout*>(pobjLayout));
                                          QGridLayout* pgrdlo(qobject_cast<QGridLayout*>(pobjLayout));
                                          QHBoxLayout* phbxlo(qobject_cast<QHBoxLayout*>(pobjLayout));
                                          QVBoxLayout* pvbxlo(qobject_cast<QVBoxLayout*>(pobjLayout));
                                          if ( pfrmlo != nullptr ) {
                                              pobjGroupBox->setLayout(pfrmlo);
                                              pfrmlo->addWidget(pobjWidget);
                                          } else if ( pgrdlo != nullptr ) {
                                              //pgrdlo->addItem()
                                          } else if ( phbxlo != nullptr ) {
                                              pobjGroupBox->setLayout(phbxlo);
                                              phbxlo->addWidget(pobjWidget);
                                          } else if ( pvbxlo != nullptr ) {
                                              pobjGroupBox->setLayout(pvbxlo);
                                              pvbxlo->addWidget(pobjWidget);
                                          }
                                      }
                                  }
                          

                          The only difference is that this code isn't a loop but part of a function thats called for each button.

                          Kind Regards,
                          Sy

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mchinand
                            wrote on last edited by
                            #15

                            @SPlatten said in Layout scratching head...:

                            QHBoxLayout* phbxLayout(new QHBoxLayout);
                            QGroupBox* pobjGroup(new QGroupBox);
                            pobjGroup->setLayout(phbxLayout);
                            for( int i=1; i<=3; i++ ) {
                                QPushButton* pbtnTemp(new QPushButton(QString("Button %1").arg(i)));
                                phbxLayout->addWidget(pbtnTemp);
                            }
                            pvbxLayout->addWidget(pobjGroup);
                            

                            In this code, you are not adding the QGroupBox to the same layout as the layout you are adding the buttons to. From what you said above and in the code below, I think you are adding the QGroupBox and buttons to the same layout.

                            SPlattenS 1 Reply Last reply
                            0
                            • M mchinand

                              @SPlatten said in Layout scratching head...:

                              QHBoxLayout* phbxLayout(new QHBoxLayout);
                              QGroupBox* pobjGroup(new QGroupBox);
                              pobjGroup->setLayout(phbxLayout);
                              for( int i=1; i<=3; i++ ) {
                                  QPushButton* pbtnTemp(new QPushButton(QString("Button %1").arg(i)));
                                  phbxLayout->addWidget(pbtnTemp);
                              }
                              pvbxLayout->addWidget(pobjGroup);
                              

                              In this code, you are not adding the QGroupBox to the same layout as the layout you are adding the buttons to. From what you said above and in the code below, I think you are adding the QGroupBox and buttons to the same layout.

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

                              @mchinand , the sample code I posted created a new layout then uses that layout in the group box, then adds widgets to the layout and finally adds the group to different vertical layout.

                              Kind Regards,
                              Sy

                              M 1 Reply Last reply
                              0
                              • SPlattenS SPlatten

                                @mchinand , the sample code I posted created a new layout then uses that layout in the group box, then adds widgets to the layout and finally adds the group to different vertical layout.

                                M Offline
                                M Offline
                                mchinand
                                wrote on last edited by
                                #17

                                @SPlatten said in Layout scratching head...:

                                @mchinand , the sample code I posted created a new layout then uses that layout in the group box, then adds widgets to the layout and finally adds the group to different vertical layout.

                                Right, your sample code is correct, but it seems your actual code is adding the qgroupbox and buttons to the same layout.

                                SPlattenS 1 Reply Last reply
                                0
                                • M mchinand

                                  @SPlatten said in Layout scratching head...:

                                  @mchinand , the sample code I posted created a new layout then uses that layout in the group box, then adds widgets to the layout and finally adds the group to different vertical layout.

                                  Right, your sample code is correct, but it seems your actual code is adding the qgroupbox and buttons to the same layout.

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

                                  @mchinand, another rewrite:

                                          QWidget* pobjWidget(mpobjWidget);
                                          if ( pobjWidget != nullptr ) {
                                              QLayout* pobjLayout(pobjWidget->layout());
                                              if ( pobjLayout == nullptr && mpobjParent != nullptr ) {
                                      //Does the parent have a layout?
                                                  if ( mpobjParent->mpobjWidget != nullptr ) {
                                                      pobjLayout = mpobjParent->mpobjWidget->layout();
                                                  }
                                              }
                                              if ( pobjLayout != nullptr ) {
                                                  pobjLayout->addWidget(pobjWidget);
                                                  pobjGroupBox->setLayout(pobjLayout);
                                              }
                                          }
                                  

                                  Result still the same...

                                  Kind Regards,
                                  Sy

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mchinand
                                    wrote on last edited by
                                    #19

                                    @SPlatten said in Layout scratching head...:

                                      if ( pobjLayout != nullptr ) {
                                                pobjLayout->addWidget(pobjWidget);
                                                pobjGroupBox->setLayout(pobjLayout);
                                            }
                                        }
                                    

                                    Is the QGroupBox ever pobjWidget for this code? You know from your sample code how the widgets (the QGroupbox and buttons) should be added to the layouts, you just need to replicate that in your code.

                                    1 Reply Last reply
                                    1
                                    • M Offline
                                      M Offline
                                      mchinand
                                      wrote on last edited by mchinand
                                      #20

                                      I don't know much about your XML generation, but what if it was like this instead? This better reflects what the code should be.

                                       	<groupbox id="btnbar">
                                       	        <layout type="horizontal" width="128">
                                       		       <button id="btnApply" group="btnbar" api="applyChanges">
                                       			          <subscriber signal="clicked" target="simon2.js@applyButton"/>
                                       		       </button>
                                       		       <button id="btnUndo" group="btnbar" api="undoChanges"/>
                                      		       <button id="btnOK" group="btnbar" api="submitAndClose"/>	
                                       	       </layout>
                                       	</groupbox>
                                      
                                      SPlattenS 1 Reply Last reply
                                      1
                                      • M mchinand

                                        I don't know much about your XML generation, but what if it was like this instead? This better reflects what the code should be.

                                         	<groupbox id="btnbar">
                                         	        <layout type="horizontal" width="128">
                                         		       <button id="btnApply" group="btnbar" api="applyChanges">
                                         			          <subscriber signal="clicked" target="simon2.js@applyButton"/>
                                         		       </button>
                                         		       <button id="btnUndo" group="btnbar" api="undoChanges"/>
                                        		       <button id="btnOK" group="btnbar" api="submitAndClose"/>	
                                         	       </layout>
                                         	</groupbox>
                                        
                                        SPlattenS Offline
                                        SPlattenS Offline
                                        SPlatten
                                        wrote on last edited by SPlatten
                                        #21

                                        @mchinand , this might look a bit over the top, I have a class clsQtLayout which is derived from QWidget, the reason for this is because the class supports layouts with a scrollable area which can only be accommodated by using a widget to contain everything.

                                        After much work the QVBoxLayout works great, I have a form layout inside that can have a fixed height and in my example contains radio buttons, it can be scrollable within the vertical layout.

                                        I have tried the same techniques for the QHBoxLayout and this is where I'm having problems, for some reason which I'm not seeing it just doesn't work. Going back to my XML:

                                        	<layout type="form" height="72" hspacing="0">
                                        		<buttongroup id="enSEX" dbfield="vcSex"/>
                                        		<radiobutton id="rdoM" group="enSEX" text="Male" default="true" position="0,0"/>
                                        		<radiobutton id="rdoF" group="enSEX" text="Female" position="1,0"/>
                                        	</layout>
                                        

                                        This XML behind the scenes creates a QVBoxLayout because the height of the required QFormLayout has a fixed height. A QWidget is created which the QScrollArea connected to. The QScrollArea is then connected to the QVBoxLayout.

                                                if ( intFixedHeight > 0 ) {
                                            //Create a vertical layout as the container for this layout
                                                    pobjVBox = new QVBoxLayout;
                                            //Create and set-up scroll area
                                                    mpobjScroller = new QScrollArea;
                                                    mpobjScroller->setWidget(pobjContainer);
                                                    mpobjScroller->setFixedHeight(intFixedHeight);
                                                    pobjVBox->addWidget(mpobjScroller);
                                                }
                                                pobjLayout = new QFormLayout;
                                            //Set-up form
                                                pobjLayout->setContentsMargins(0,0,0,0);
                                                pobjLayout->setSpacing(0);
                                                QString strHorzSpacing(mpobjNode->strGetAttribute(
                                                                                clsXMLnode::mscszAttrSpacingH));
                                                if ( strHorzSpacing.isEmpty() != true ) {
                                                    ((QFormLayout*)pobjLayout)->setHorizontalSpacing(strHorzSpacing.toInt());
                                                }
                                                if ( pobjContainer != nullptr ) {
                                                    pobjContainer->setLayout(pobjLayout);
                                                }
                                        

                                        This all works fine, the following XML is for the horizontal layout:

                                        	<layout id="btnbarLO" type="horizontal" width="128">
                                        		<groupbox id="btnbar" layout="btnbarLO" properties="background-color:#ff0000;"/>
                                        		<button id="btnApply" group="btnbar" api="applyChanges">
                                        			<subscriber signal="clicked" target="simon2.js@applyButton"/>
                                        		</button>
                                        		<button id="btnUndo" group="btnbar" api="undoChanges"/>
                                        		<button id="btnOK" group="btnbar" api="submitAndClose"/>	
                                        	</layout>
                                        

                                        And the code in the layout class for this, which should be very similar to the vertical layout:

                                                if ( intFixedWidth > 0 ) {
                                            //Create a vertical layout as the container for this layout
                                                    pobjHBox = new QHBoxLayout;
                                            //Create and set-up scroll area
                                                    mpobjScroller = new QScrollArea;
                                                    mpobjScroller->setWidget(pobjContainer);
                                                    mpobjScroller->setFixedWidth(intFixedWidth);
                                                    pobjHBox->addWidget(mpobjScroller);
                                                }
                                                pobjLayout = new QHBoxLayout;
                                            //Set-up form
                                                pobjLayout->setContentsMargins(0,0,0,0);
                                                pobjLayout->setSpacing(0);
                                                QString strHorzSpacing(mpobjNode->strGetAttribute(
                                                                                clsXMLnode::mscszAttrSpacingH));
                                                if ( strHorzSpacing.isEmpty() != true ) {
                                                    ((QHBoxLayout*)pobjLayout)->addSpacing(strHorzSpacing.toInt());
                                                }
                                                if ( pobjContainer != nullptr ) {
                                                    pobjContainer->setLayout(pobjLayout);
                                                }
                                        

                                        However this isn't working and the results are as shown in the original screenshot:
                                        Screenshot 2021-11-12 at 08.09.31.png

                                        I've set the background colour of the QGroupBox to red to clarify where it is.

                                        [edit] I can see it isn't clear, so elsewhere in my node handling code there is this logic where nodes are appended to parent nodes:

                                            QWidget* pobjWChild(pobjChild->pobjGetWidget());
                                            if ( pobjWChild != nullptr && mstrName.compare(clsXMLnode::mscszNodeLayout) == 0 ) {
                                            //Yes, does the parent have a layout?
                                                QLayout* pobjLayout(pobjGetLayout());
                                                if ( pobjLayout != nullptr ) {
                                                    QString strType(strGetAttribute(clsXMLnode::mscszAttrType));
                                                    QFormLayout* pobjForm(qobject_cast<QFormLayout*>(pobjLayout));
                                            //Add the widget for the layout
                                                    if ( pobjForm != nullptr
                                                      && strType.compare(clsXMLnode::mscszLayoutVertical) == 0 ) {
                                                        pobjForm->addRow(pobjWChild);
                                                    } else {
                                                        pobjLayout->addWidget(pobjWChild);
                                                    }
                                                }
                                            }
                                        

                                        Kind Regards,
                                        Sy

                                        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