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. Resizing QLineEdit in a QGridLayout
Forum Updated to NodeBB v4.3 + New Features

Resizing QLineEdit in a QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 2.1k 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.
  • jsulmJ jsulm

    @swurl Take a look at https://doc.qt.io/qt-6/qgridlayout.html#setColumnStretch

    S Offline
    S Offline
    swurl
    wrote on last edited by
    #3

    @jsulm What values should I input? I tried values of 0, 1, 100, etc. and nothing changes.

    M jsulmJ 2 Replies Last reply
    0
    • S swurl

      @jsulm What values should I input? I tried values of 0, 1, 100, etc. and nothing changes.

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #4

      @swurl
      Try to set the horizontal size policy of the lineEdit to expanding.
      You can test this kind of things in the Designer.

      S 1 Reply Last reply
      0
      • M mpergand

        @swurl
        Try to set the horizontal size policy of the lineEdit to expanding.
        You can test this kind of things in the Designer.

        S Offline
        S Offline
        swurl
        wrote on last edited by swurl
        #5

        @mpergand This did not work either.

        Interestingly I tried setting the vertical size policy as well, and setting that to expanding resulted in this:

        d018176e-4b7f-4027-a0a7-36b7b56f99c6-image.png

        (this is with both horizontal AND vertical policies of both QLineEdits set to QSizePolicy::Expanding.)

        S 1 Reply Last reply
        0
        • S swurl

          @mpergand This did not work either.

          Interestingly I tried setting the vertical size policy as well, and setting that to expanding resulted in this:

          d018176e-4b7f-4027-a0a7-36b7b56f99c6-image.png

          (this is with both horizontal AND vertical policies of both QLineEdits set to QSizePolicy::Expanding.)

          S Offline
          S Offline
          swurl
          wrote on last edited by
          #6

          @swurl OK, something even weirder:

          When I use the same code as the previous picture but remove the Qt::AlignLeft alignment, I get this:

          6f293965-7e76-464c-94b7-4c6844b42f39-image.png

          However it should be "Test2" that takes up most of the space, not the first one.

          1 Reply Last reply
          0
          • S swurl

            @jsulm What values should I input? I tried values of 0, 1, 100, etc. and nothing changes.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @swurl said in Resizing QLineEdit in a QGridLayout:

            What values should I input?

            The documentation is quite clear:
            "The stretch factor is relative to the other columns in this grid. Columns with a higher stretch factor take more of the available space."
            So, if one should be double size compared to the other then use 1 and 2.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • jsulmJ jsulm

              @swurl said in Resizing QLineEdit in a QGridLayout:

              What values should I input?

              The documentation is quite clear:
              "The stretch factor is relative to the other columns in this grid. Columns with a higher stretch factor take more of the available space."
              So, if one should be double size compared to the other then use 1 and 2.

              S Offline
              S Offline
              swurl
              wrote on last edited by
              #8

              @jsulm Understood.

              Any idea what's going on with the image in my last post? Seems like the second line edit should be taking up twice the space. Using a column stretch works, but ideally I'd like to avoid the column stretch as my ultimate goal would be to have these line edits take up space properly. For example (from a non-Qt application):

              5040065f-622d-4d36-b26f-91759e0d6a99-image.png

              However this is obviously not possible with column stretches, as the two different rows would have different column stretches. Ideally, changing the column spans would work, but as shown in my previous post that did not work.

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

                Hi,

                If you want your widgets to take different spaces on each lines, you might want to consider using a QVBoxLayout/QHBoxLayout combo as you do not seem to work with a grid in fact.

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

                S 1 Reply Last reply
                2
                • SGaistS SGaist

                  Hi,

                  If you want your widgets to take different spaces on each lines, you might want to consider using a QVBoxLayout/QHBoxLayout combo as you do not seem to work with a grid in fact.

                  S Offline
                  S Offline
                  swurl
                  wrote on last edited by
                  #10

                  @SGaist Okay I see. I was thinking of doing this but how would I have a widget take up two rows?

                  jsulmJ 1 Reply Last reply
                  0
                  • S swurl

                    @SGaist Okay I see. I was thinking of doing this but how would I have a widget take up two rows?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @swurl said in Resizing QLineEdit in a QGridLayout:

                    how would I have a widget take up two rows?

                    There are no real rows with the approach from @SGaist
                    So, the question is not clear.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @swurl said in Resizing QLineEdit in a QGridLayout:

                      how would I have a widget take up two rows?

                      There are no real rows with the approach from @SGaist
                      So, the question is not clear.

                      S Offline
                      S Offline
                      swurl
                      wrote on last edited by
                      #12

                      @jsulm For example, a widget taking up two rows and two columns in a QGridLayout, but in the VBox-HBox combo

                      My only idea on doing this with a VBox-HBox combination would be stretching the HBox to be two "rows" tall, but then you couldn't have another one-row-tall widget next to that widget, it'd have to be two "rows" tall.

                      I feel like I'm thinking of this wrong... There should definitely be a way to have widgets take up different spaces on different lines that I'm missing.

                      jsulmJ 1 Reply Last reply
                      0
                      • S swurl

                        @jsulm For example, a widget taking up two rows and two columns in a QGridLayout, but in the VBox-HBox combo

                        My only idea on doing this with a VBox-HBox combination would be stretching the HBox to be two "rows" tall, but then you couldn't have another one-row-tall widget next to that widget, it'd have to be two "rows" tall.

                        I feel like I'm thinking of this wrong... There should definitely be a way to have widgets take up different spaces on different lines that I'm missing.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @swurl You can use https://doc.qt.io/qt-6/qboxlayout.html#setStretch

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        S 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @swurl You can use https://doc.qt.io/qt-6/qboxlayout.html#setStretch

                          S Offline
                          S Offline
                          swurl
                          wrote on last edited by swurl
                          #14

                          @jsulm OK, with that I think I've gotten closer.

                          Here's an example of what I would like the final potential layout to look like (again, non-Qt):

                          f10a98d3-6372-4f0a-a0bb-e24fbd045e5b-image.png

                          This is the closest I can get: (notice that the Test2 box takes up two "rows", not one)

                          8b8fdb3f-073e-42fb-94f0-50944fa3e0a1-image.png

                          This is with this code in a QMainWindow derivative:

                          QWidget *widget = new QWidget;
                              setCentralWidget(widget);
                              QLineEdit *edit = new QLineEdit("Test");
                              QLineEdit *edit2 = new QLineEdit("Test2");
                              QLineEdit *edit3 = new QLineEdit("Test3");
                              QLineEdit *edit4 = new QLineEdit("Test4");
                          
                              QVBoxLayout *layout = new QVBoxLayout(widget);
                          
                              QHBoxLayout *h1 = new QHBoxLayout;
                          
                              QHBoxLayout *h2 = new QHBoxLayout;
                          
                              layout->addLayout(h1, 1);
                              layout->addLayout(h2, 1);
                          
                              h1->addWidget(edit, 2);
                              h1->addWidget(edit2, 1);
                              h2->addWidget(edit3, 1);
                              h2->addWidget(edit4, 2);
                          
                              layout->setStretch(layout->indexOf(h1), 2);
                          
                              edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                              edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                              edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                              edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                          

                          I imagine there's a way to do this with the geometry of edit2, the one I don't want to take up two "rows", like setting the height to half of its initial height, but that has its drawbacks too.

                          M 1 Reply Last reply
                          0
                          • S swurl

                            @jsulm OK, with that I think I've gotten closer.

                            Here's an example of what I would like the final potential layout to look like (again, non-Qt):

                            f10a98d3-6372-4f0a-a0bb-e24fbd045e5b-image.png

                            This is the closest I can get: (notice that the Test2 box takes up two "rows", not one)

                            8b8fdb3f-073e-42fb-94f0-50944fa3e0a1-image.png

                            This is with this code in a QMainWindow derivative:

                            QWidget *widget = new QWidget;
                                setCentralWidget(widget);
                                QLineEdit *edit = new QLineEdit("Test");
                                QLineEdit *edit2 = new QLineEdit("Test2");
                                QLineEdit *edit3 = new QLineEdit("Test3");
                                QLineEdit *edit4 = new QLineEdit("Test4");
                            
                                QVBoxLayout *layout = new QVBoxLayout(widget);
                            
                                QHBoxLayout *h1 = new QHBoxLayout;
                            
                                QHBoxLayout *h2 = new QHBoxLayout;
                            
                                layout->addLayout(h1, 1);
                                layout->addLayout(h2, 1);
                            
                                h1->addWidget(edit, 2);
                                h1->addWidget(edit2, 1);
                                h2->addWidget(edit3, 1);
                                h2->addWidget(edit4, 2);
                            
                                layout->setStretch(layout->indexOf(h1), 2);
                            
                                edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                            

                            I imagine there's a way to do this with the geometry of edit2, the one I don't want to take up two "rows", like setting the height to half of its initial height, but that has its drawbacks too.

                            M Offline
                            M Offline
                            mpergand
                            wrote on last edited by mpergand
                            #15

                            @swurl said in Resizing QLineEdit in a QGridLayout:

                            layout->setStretch(layout->indexOf(h1), 2);

                            If I understand you well, I think the strech should be applied to h2 not h1.

                            Is it what you want :

                            If so, you need to add a second vertical layout and insert a dummy widget after test2:

                            
                            QLineEdit *edit = new QLineEdit("Test");
                               QLineEdit *edit2 = new QLineEdit("Test2");
                               QLineEdit *edit3 = new QLineEdit("Test3");
                               QLineEdit *edit4 = new QLineEdit("Test4");
                            
                               QVBoxLayout *layout = new QVBoxLayout(widget);
                               QHBoxLayout *h1 = new QHBoxLayout;
                               QHBoxLayout *h2 = new QHBoxLayout;
                               QVBoxLayout *v2 = new QVBoxLayout(widget);
                            
                               layout->addLayout(h1, 1);
                               h1->addWidget(edit, 2);
                            	h1->addLayout(v2);	// add vertical layout at index 1
                            	v2->addWidget(edit2);
                            	auto dummy=new QWidget;	// add a dummy widget
                            	dummy->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                            	v2->addWidget(dummy);
                            
                            	layout->addLayout(h2, 1);
                            
                               h2->addWidget(edit3, 1);
                               h2->addWidget(edit4, 2);
                            
                               layout->setStretch(layout->indexOf(h1), 2);
                            
                               edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                               edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                               edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                               edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                            
                            
                            S 1 Reply Last reply
                            0
                            • M mpergand

                              @swurl said in Resizing QLineEdit in a QGridLayout:

                              layout->setStretch(layout->indexOf(h1), 2);

                              If I understand you well, I think the strech should be applied to h2 not h1.

                              Is it what you want :

                              If so, you need to add a second vertical layout and insert a dummy widget after test2:

                              
                              QLineEdit *edit = new QLineEdit("Test");
                                 QLineEdit *edit2 = new QLineEdit("Test2");
                                 QLineEdit *edit3 = new QLineEdit("Test3");
                                 QLineEdit *edit4 = new QLineEdit("Test4");
                              
                                 QVBoxLayout *layout = new QVBoxLayout(widget);
                                 QHBoxLayout *h1 = new QHBoxLayout;
                                 QHBoxLayout *h2 = new QHBoxLayout;
                                 QVBoxLayout *v2 = new QVBoxLayout(widget);
                              
                                 layout->addLayout(h1, 1);
                                 h1->addWidget(edit, 2);
                              	h1->addLayout(v2);	// add vertical layout at index 1
                              	v2->addWidget(edit2);
                              	auto dummy=new QWidget;	// add a dummy widget
                              	dummy->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                              	v2->addWidget(dummy);
                              
                              	layout->addLayout(h2, 1);
                              
                                 h2->addWidget(edit3, 1);
                                 h2->addWidget(edit4, 2);
                              
                                 layout->setStretch(layout->indexOf(h1), 2);
                              
                                 edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                 edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                 edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                 edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                              
                              
                              S Offline
                              S Offline
                              swurl
                              wrote on last edited by
                              #16

                              @mpergand Thank you, this will work for me!

                              Now to write a class to handle this for me. :)

                              1 Reply Last reply
                              0
                              • S swurl has marked this topic as solved on
                              • S Offline
                                S Offline
                                swurl
                                wrote on last edited by
                                #17

                                OK, outside of a custom class. is there any way to achieve what I'm looking for? Seems unlikely to me that Qt is unable to properly handle custom spans and layout everything equally with only a QGridLayout, but work totally fine with box layouts.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  swurl
                                  wrote on last edited by
                                  #18

                                  After some experimentation, I've finally got the "ideal" solution:

                                  If you want to use colspan and rowspan in the same way I'm looking for, then every row and column that is stretched through must have a stretch set to 1. Final code:

                                  QWidget *widget = new QWidget;
                                  setCentralWidget(widget);
                                  
                                  QLineEdit *edit = new QLineEdit("Test");
                                  QLineEdit *edit2 = new QLineEdit("Test2");
                                  QLineEdit *edit3 = new QLineEdit("Test3");
                                  QLineEdit *edit4 = new QLineEdit("Test4");
                                  
                                  QGridLayout *layout = new QGridLayout(widget);
                                  
                                  layout->setColumnStretch(0, 1);
                                  layout->setColumnStretch(1, 1);
                                  layout->setColumnStretch(2, 1);
                                  
                                  layout->setRowStretch(0, 1);
                                  layout->setRowStretch(1, 1);
                                  layout->setRowStretch(2, 1);
                                  
                                  layout->addWidget(edit, 0, 0, 2, 2);
                                  layout->addWidget(edit2, 0, 2, 1, 1);
                                  layout->addWidget(edit3, 2, 0, 1, 1);
                                  layout->addWidget(edit4, 2, 1, 1, 2);
                                  
                                  edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                  edit2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                  edit3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                  edit4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                  

                                  Results in:

                                  79867660-3aec-4718-ac61-b418cb3aa3c2-image.png

                                  Before I had assumed that the default stretch of every widget in a layout was 1, but nope. This, however, fixes it. Also ensure that every column and row that is spanned at all is stretched (i.e. column 0 with span 2 = 0 and 1 need to be stretched to 1).

                                  1 Reply Last reply
                                  1
                                  • S swurl has marked this topic as solved on

                                  • Login

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