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

Resizing QLineEdit in a QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 2.0k 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.
  • S Offline
    S Offline
    swurl
    wrote on 1 Oct 2023, 23:59 last edited by
    #1

    In a QGridLayout, I am not able to resize a QLineEdit or set its minimum width.

    What I'm looking for is that the following code (in a QMainWindow subclass)...

    QWidget *widget = new QWidget;
    setCentralWidget(widget);
    QLineEdit *edit = new QLineEdit("Test");
    QLineEdit *edit2 = new QLineEdit("Test2");
    
    QGridLayout *layout = new QGridLayout(widget);
    
    layout->addWidget(edit, 0, 0, 1, 1, Qt::AlignLeft);
    layout->addWidget(edit2, 0, 1, 1, 100, Qt::AlignLeft);
    

    ...would result in the first QLineEdit showing very little text, i.e. one character, and the other QLineEdit stretched out to fit most of the rest of the available space.

    However, what ultimately shows up is this:

    2e5d6e5e-c76a-49de-b91d-928868c40f43-image.png

    I have tried setting the size policy, column minimum width and line edit minimum width, changing the size hint, and the stylesheet, but neither line edit ever changed its size once. How would I go about achieving this?

    J 1 Reply Last reply 2 Oct 2023, 05:19
    0
    • S Offline
      S Offline
      swurl
      wrote on 23 Oct 2023, 18:12 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
        1 Oct 2023, 23:59

        In a QGridLayout, I am not able to resize a QLineEdit or set its minimum width.

        What I'm looking for is that the following code (in a QMainWindow subclass)...

        QWidget *widget = new QWidget;
        setCentralWidget(widget);
        QLineEdit *edit = new QLineEdit("Test");
        QLineEdit *edit2 = new QLineEdit("Test2");
        
        QGridLayout *layout = new QGridLayout(widget);
        
        layout->addWidget(edit, 0, 0, 1, 1, Qt::AlignLeft);
        layout->addWidget(edit2, 0, 1, 1, 100, Qt::AlignLeft);
        

        ...would result in the first QLineEdit showing very little text, i.e. one character, and the other QLineEdit stretched out to fit most of the rest of the available space.

        However, what ultimately shows up is this:

        2e5d6e5e-c76a-49de-b91d-928868c40f43-image.png

        I have tried setting the size policy, column minimum width and line edit minimum width, changing the size hint, and the stylesheet, but neither line edit ever changed its size once. How would I go about achieving this?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 2 Oct 2023, 05:19 last edited by
        #2

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

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

        S 1 Reply Last reply 3 Oct 2023, 20:11
        1
        • J jsulm
          2 Oct 2023, 05:19

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

          S Offline
          S Offline
          swurl
          wrote on 3 Oct 2023, 20:11 last edited by
          #3

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

          M J 2 Replies Last reply 3 Oct 2023, 21:56
          0
          • S swurl
            3 Oct 2023, 20:11

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

            M Offline
            M Offline
            mpergand
            wrote on 3 Oct 2023, 21:56 last edited by mpergand 10 Apr 2023, 07:59
            #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 6 Oct 2023, 17:48
            0
            • M mpergand
              3 Oct 2023, 21:56

              @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 6 Oct 2023, 17:48 last edited by swurl 10 Jun 2023, 17:49
              #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 6 Oct 2023, 18:04
              0
              • S swurl
                6 Oct 2023, 17:48

                @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 6 Oct 2023, 18:04 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
                  3 Oct 2023, 20:11

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 6 Oct 2023, 18:32 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 9 Oct 2023, 15:52
                  0
                  • J jsulm
                    6 Oct 2023, 18:32

                    @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 9 Oct 2023, 15:52 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 9 Oct 2023, 18:29 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 9 Oct 2023, 20:15
                      2
                      • SGaistS SGaist
                        9 Oct 2023, 18:29

                        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 9 Oct 2023, 20:15 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?

                        J 1 Reply Last reply 9 Oct 2023, 20:45
                        0
                        • S swurl
                          9 Oct 2023, 20:15

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

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 9 Oct 2023, 20:45 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 9 Oct 2023, 20:52
                          0
                          • J jsulm
                            9 Oct 2023, 20:45

                            @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 9 Oct 2023, 20:52 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.

                            J 1 Reply Last reply 9 Oct 2023, 23:14
                            0
                            • S swurl
                              9 Oct 2023, 20:52

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

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 9 Oct 2023, 23:14 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 10 Oct 2023, 18:45
                              0
                              • J jsulm
                                9 Oct 2023, 23:14

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

                                S Offline
                                S Offline
                                swurl
                                wrote on 10 Oct 2023, 18:45 last edited by swurl 10 Oct 2023, 18:46
                                #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 10 Oct 2023, 19:13
                                0
                                • S swurl
                                  10 Oct 2023, 18:45

                                  @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 10 Oct 2023, 19:13 last edited by mpergand 10 Oct 2023, 19:48
                                  #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 11 Oct 2023, 15:28
                                  0
                                  • M mpergand
                                    10 Oct 2023, 19:13

                                    @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 11 Oct 2023, 15:28 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 11 Oct 2023, 15:28
                                    • S Offline
                                      S Offline
                                      swurl
                                      wrote on 16 Oct 2023, 19:00 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 23 Oct 2023, 18:12 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 28 Dec 2024, 00:12

                                        • Login

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