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
QtWS25 Last Chance

QGroupBox does not fit into layouts

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgroupboxqhboxlayoutqvboxlayoutqt5.6.1
7 Posts 3 Posters 5.1k Views
  • 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.
  • P Offline
    P Offline
    pauledd
    wrote on 10 Aug 2016, 15:02 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
    • V Offline
      V Offline
      VRonin
      wrote on 10 Aug 2016, 15:14 last edited by VRonin 8 Oct 2016, 15:15
      #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
      • P Offline
        P Offline
        pauledd
        wrote on 10 Aug 2016, 16:29 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
        • P Offline
          P Offline
          pauledd
          wrote on 10 Aug 2016, 16:35 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Aug 2016, 21:41 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
            • V Offline
              V Offline
              VRonin
              wrote on 11 Aug 2016, 06:53 last edited by VRonin 8 Nov 2016, 06:54
              #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
              • P Offline
                P Offline
                pauledd
                wrote on 11 Aug 2016, 15:17 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

                2/7

                10 Aug 2016, 15:14

                topic:navigator.unread, 5
                • Login

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