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