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. QGroupBox does not fit into layouts

QGroupBox does not fit into layouts

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgroupboxqhboxlayoutqvboxlayoutqt5.6.1
7 Posts 3 Posters 5.8k Views 1 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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on last edited by
    #1

    Hi
    I have a QDialog in which is a QTabWidget. In this widget should be some groups. One group is "Settings" that consists of a qlabel and a qlineedit per row. I will add more pairs later. I want to have a border arround this group so I thought qgroupbox might be proper. I simply dont get behind the qgroupbox magic... I dont know how to shrink the Settings-qgroupbox to the size of the label and the lineedit. I managed to shrink it vertically but it does not horizontally.
    Here is an image how it looks current.
    https://pauledd.files.wordpress.com/2016/08/pat.png

    GeneralTab::GeneralTab(QWidget *parent)
    	:QWidget(parent)
    {	
    	QLabel *videoDeviceLabel = new QLabel("Video Device:");
    	QLineEdit *videoDeviceValueLabel = new QLineEdit("/dev/video0");
    	QHBoxLayout *horizontalLayout = new QHBoxLayout;
    	horizontalLayout->addWidget(videoDeviceLabel);
    	horizontalLayout->addWidget(videoDeviceValueLabel);
    	horizontalLayout->addStretch(1);
    	QGroupBox *box = new QGroupBox("Settings");
    	QVBoxLayout *mainLayout = new QVBoxLayout;
    	mainLayout->addWidget(box);
    	mainLayout->addStretch(1);
    	box->setLayout(horizontalLayout);
    	setLayout(mainLayout);
    }
    

    any ideas?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      it's not a problem of the groupbox but of the layout you put it in

      change

          QVBoxLayout *mainLayout = new QVBoxLayout;
          mainLayout->addWidget(box);
          mainLayout->addStretch(1);
      

      into

          QGridLayout *mainLayout = new QGridLayout ;
          mainLayout->addWidget(box,0,0);
          mainLayout->additem(new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePolicy::Preferred),0,1);
      mainLayout->additem(new QSpacerItem(10,10,QSizePolicy::Preferred,QSizePolicy::Expanding),1,0);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • pauleddP Offline
        pauleddP Offline
        pauledd
        wrote on last edited by
        #3

        Ok, now I get this:
        https://pauledd.files.wordpress.com/2016/08/pat2.png
        The GroupBox seems to horizontally take half of the dialog but does not shrink to the end
        of the qlineedit.

        1 Reply Last reply
        0
        • pauleddP Offline
          pauleddP Offline
          pauledd
          wrote on last edited by
          #4

          I just tried a new project and just by using the form editor and that was so easy:
          https://pauledd.files.wordpress.com/2016/08/pat3.png

          Its just a button and a lineedit in a horizontal layout in a group box. Why cant I do this programmatically?

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

            Hi,

            Since you want a QLabel + QLineEdit, why not use QFromLayout ?

            That should also simplify size handling.

            Hope it helps

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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              remove horizontalLayout->addStretch(1); that is what is taking space after the line edit

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • pauleddP Offline
                pauleddP Offline
                pauledd
                wrote on last edited by
                #7

                @SGaist thank you, I took QFormLayout into account.
                @VRonin also thank you!
                I can not believe that it would take so much code to get this done...
                After starting over and over again I came up with this:

                GeneralTab::GeneralTab(QWidget *parent)
                	:QWidget(parent)
                {	
                			QLineEdit *lineEdit = new QLineEdit("/dev/video0");
                			QGroupBox *groupBox = new QGroupBox("Settings",this);
                			groupBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
                			QFormLayout *formLayout = new QFormLayout(groupBox);
                			formLayout->addRow("Video Device: ",lineEdit);
                }
                

                and this looks exactly what I wanted, two widgets with a nice group box border sized to minimal space usage:
                https://pauledd.files.wordpress.com/2016/08/pat4.png
                I dont really know If this code is correct or might produce trouble but at least I dont find any errors at the moment.

                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