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. Minimizing Vertical Spacing in QGridLayout
Forum Updated to NodeBB v4.3 + New Features

Minimizing Vertical Spacing in QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 15.4k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Hi,

    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    

    should do what you want.

    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
    2
    • A Offline
      A Offline
      Absurd
      wrote on last edited by
      #5

      Thank you SGaist.
      Setting:

      layout->setContentsMargins(0, 0, 0, 0);
      layout->setSpacing(0);
      

      Did not remove the margins...

      Here's what I have:

      PacketHeader::PacketHeader(QWidget* parent, const QString &type) : QWidget(parent) {
      
          QGridLayout* header = new QGridLayout;
      
          for (unsigned row = 0; row < 4; row++) {
              for (unsigned column = 0; column < 32; column++) {
                  Bit* bit = new Bit;
                  header->addWidget(bit, row, column);
              }
          }
      
          header->setSpacing(0);
          header->setContentsMargins(0, 0, 0, 0);
      
          headerType = new QLabel(type);
      
          QVBoxLayout* mainLayout = new QVBoxLayout;
          mainLayout->addWidget(headerType);
          mainLayout->addLayout(header);
      
          setLayout(mainLayout);
          setWindowTitle("Header");
      }
      

      Where Bit (inherits from QLineEdit) has the following in its constructor:

      Bit::Bit(QWidget* parent) : QLineEdit(parent) {
          QFont font("Courier New", 10, QFont::Bold);
          setFont(font);
          setMaxLength(1);
          setFrame(false);
          setFixedWidth(16);
          setAlignment(Qt::AlignCenter);
          setText("0");
      }
      

      And that's the result:

      0_1544946539295_e7a8b179-b917-4a8c-a983-c9cdeba21c00-image.png

      Are you sure that the margin I see is not a margin that's being claimed by the QLineEdit (rather than the QGridLayout)?

      I can upload the full code if needed, although there's not much more than the above...

      Thanks.

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

        What desktop environment are you using ?

        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
        • A Offline
          A Offline
          Absurd
          wrote on last edited by Absurd
          #7

          It's a remote server that I am connecting to through ssh, it's running linux.

          $ uname -a
          Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
          

          (Is that what you meant by "desktop environment"?)

          SGaistS 1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @Absurd
            Hi
            If there are more room left after all lineEdits are layout, there will still be space.
            QLineEdit do not claim any extra space.
            I had to insert a spacer to compress them or else the layout would divide extra space
            amount the lineedits.
            alt text

            So if you window/layout area is not 100% the size of one row of lineEdits, it might add some space due to you setting fixed size on the lineEdits.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              Absurd
              wrote on last edited by Absurd
              #9

              I did nothing special on the QGridLayout nor on the QLineEdit...
              You can see the constructor for the main Widget above (PacketHeader), and even when commenting all size-related lines in the Bit constructor (which is actually a QLineEdit):

              Bit::Bit(QWidget* parent) : QLineEdit(parent) {
                  QFont font("Courier New", 10, QFont::Bold);
                  setFont(font);
                  //setMaxLength(1);
                  setFrame(false);
                  //setFixedWidth(16);
                  setAlignment(Qt::AlignCenter);
                  setText("0");
              }
              

              I am still left with this:
              0_1544962979993_ca034ea7-e723-452e-acf2-013c272ace57-image.png

              Here's the main entry for the window, in case anyone kindly wants to try running:

              int main(int argc, char* argv[]) {
                  QApplication app(argc, argv);
              
                  PacketHeader element;
                  element.show();
              
                  return app.exec();
              }
              

              Thanks.

              edit
              My goal is to have 4 x 32 one-character boxes such that the boxes will be packed together with no space between them.

              edit 2
              mrjj, I am guessing that you are using some kind of Designer Tool, can you please share the code that's generated? Maybe I can take a look at it as a reference to understand what I am doing wrong...

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #10

                Hi
                Yes, it the one build into Creator.
                Anyway, i fiddled around with it and came up with

                QLineEdit *MainWindow::MakeEdit()
                {
                    QLineEdit *line = new QLineEdit();
                    line->setMaxLength(1);
                    line->setFrame(false);
                    line->setFixedSize(QSize(16, 16));
                    line->setAlignment(Qt::AlignCenter);
                    line->setText("0");
                    return line;
                }
                
                void MainWindow::SetupGrid(QGridLayout *layout )
                {
                    layout->setContentsMargins(0, 0, 0, 0);
                    layout->setSpacing(0);
                    int rowCount=4;
                    int colCount=32;
                    for (int row = 1; row <= rowCount; ++row) {
                        for (int col = 1; col <= colCount; ++col) {
                            layout->addWidget( MakeEdit(), row, col );
                        }
                    }
                
                    auto horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
                    layout->addItem(horizontalSpacer, 0, colCount+1, 1, 1);
                
                
                    auto verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
                    layout->addItem(verticalSpacer, rowCount+1, 0, 1, 1);
                }
                
                void MainWindow::on_pushButton_released()
                {
                    SetupGrid(ui->gridLayout);
                }
                
                

                Which seems to hold them together, even when window is resized so
                layout has far more space to the right.
                alt text

                test project
                https://www.dropbox.com/s/la5qrlz48nlwr9p/TonsOfEdit.zip?dl=0

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  Absurd
                  wrote on last edited by
                  #11

                  :(
                  I changed my Bit imp. to this:

                  Bit::Bit(QWidget* parent) : QLineEdit(parent) {
                      //QFont font("Courier New", 10, QFont::Bold);
                      //setFont(font);
                      setMaxLength(1);
                      setFrame(false);
                      //setFixedWidth(16);
                      setFixedSize(QSize(16, 16));
                      setAlignment(Qt::AlignCenter);
                      setText("0");
                  }
                  

                  (basically exactly like yours word-for-word - I even ruled out the possibility that it is somehow the QFonts fault), but still the same:

                  0_1544981251641_f6102087-fd0f-4610-a067-5520e922acb8-image.png

                  mrjjM 1 Reply Last reply
                  0
                  • A Absurd

                    :(
                    I changed my Bit imp. to this:

                    Bit::Bit(QWidget* parent) : QLineEdit(parent) {
                        //QFont font("Courier New", 10, QFont::Bold);
                        //setFont(font);
                        setMaxLength(1);
                        setFrame(false);
                        //setFixedWidth(16);
                        setFixedSize(QSize(16, 16));
                        setAlignment(Qt::AlignCenter);
                        setText("0");
                    }
                    

                    (basically exactly like yours word-for-word - I even ruled out the possibility that it is somehow the QFonts fault), but still the same:

                    0_1544981251641_f6102087-fd0f-4610-a067-5520e922acb8-image.png

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @Absurd
                    And you use QSpacerItem too ??
                    Thats so odd then.

                    1 Reply Last reply
                    0
                    • A Absurd

                      It's a remote server that I am connecting to through ssh, it's running linux.

                      $ uname -a
                      Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
                      

                      (Is that what you meant by "desktop environment"?)

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      @Absurd said in Minimizing Vertical Spacing in QGridLayout:

                      It's a remote server that I am connecting to through ssh, it's running linux.

                      $ uname -a
                      Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
                      

                      (Is that what you meant by "desktop environment"?)

                      No that's not, examples of desktop environments:

                      • KDE
                      • Gnome
                      • LXDE
                        etc.

                      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
                      • A Offline
                        A Offline
                        Absurd
                        wrote on last edited by
                        #14

                        @mrjj ; no QSpacerItem. I'll try adding that two and will update.
                        @SGaist ; it's GNOME.

                        mrjjM 1 Reply Last reply
                        0
                        • A Absurd

                          @mrjj ; no QSpacerItem. I'll try adding that two and will update.
                          @SGaist ; it's GNOME.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          @Absurd
                          The spacer item is the key. else it will distribute the remaining space among the lineEdits.
                          its how layout works.

                          1 Reply Last reply
                          1
                          • A Offline
                            A Offline
                            Absurd
                            wrote on last edited by Absurd
                            #16

                            But that's the thing; why is there remaining space in the first place, if I used QGridLayout.setSpacing(0)?

                            J.HilkJ 1 Reply Last reply
                            0
                            • A Absurd

                              But that's the thing; why is there remaining space in the first place, if I used QGridLayout.setSpacing(0)?

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #17

                              @Absurd
                              because you do set a fixed size to the content of the layout. If the content can not fill the layout, margins ans spacings are added, or what would you expect to happen?


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              1
                              • A Offline
                                A Offline
                                Absurd
                                wrote on last edited by
                                #18

                                @J-Hilk see what I wrote above:

                                @Absurd said in Minimizing Vertical Spacing in QGridLayout:

                                I did nothing special on the QGridLayout nor on the QLineEdit...
                                You can see the constructor for the main Widget above (PacketHeader), and even when commenting all size-related lines in the Bit constructor (which is actually a QLineEdit):

                                Bit::Bit(QWidget* parent) : QLineEdit(parent) {
                                    QFont font("Courier New", 10, QFont::Bold);
                                    setFont(font);
                                    //setMaxLength(1);
                                    setFrame(false);
                                    //setFixedWidth(16);
                                    setAlignment(Qt::AlignCenter);
                                    setText("0");
                                }
                                

                                I am still left with this:
                                0_1544962979993_ca034ea7-e723-452e-acf2-013c272ace57-image.png

                                Here's the main entry for the window, in case anyone kindly wants to try running:

                                int main(int argc, char* argv[]) {
                                    QApplication app(argc, argv);
                                
                                    PacketHeader element;
                                    element.show();
                                
                                    return app.exec();
                                }
                                

                                Thanks.

                                edit
                                My goal is to have 4 x 32 one-character boxes such that the boxes will be packed together with no space between them.

                                edit 2
                                mrjj, I am guessing that you are using some kind of Designer Tool, can you please share the code that's generated? Maybe I can take a look at it as a reference to understand what I am doing wrong...

                                Specifically:

                                and even when commenting all size-related lines in the Bit constructor

                                J.HilkJ 1 Reply Last reply
                                0
                                • A Absurd

                                  @J-Hilk see what I wrote above:

                                  @Absurd said in Minimizing Vertical Spacing in QGridLayout:

                                  I did nothing special on the QGridLayout nor on the QLineEdit...
                                  You can see the constructor for the main Widget above (PacketHeader), and even when commenting all size-related lines in the Bit constructor (which is actually a QLineEdit):

                                  Bit::Bit(QWidget* parent) : QLineEdit(parent) {
                                      QFont font("Courier New", 10, QFont::Bold);
                                      setFont(font);
                                      //setMaxLength(1);
                                      setFrame(false);
                                      //setFixedWidth(16);
                                      setAlignment(Qt::AlignCenter);
                                      setText("0");
                                  }
                                  

                                  I am still left with this:
                                  0_1544962979993_ca034ea7-e723-452e-acf2-013c272ace57-image.png

                                  Here's the main entry for the window, in case anyone kindly wants to try running:

                                  int main(int argc, char* argv[]) {
                                      QApplication app(argc, argv);
                                  
                                      PacketHeader element;
                                      element.show();
                                  
                                      return app.exec();
                                  }
                                  

                                  Thanks.

                                  edit
                                  My goal is to have 4 x 32 one-character boxes such that the boxes will be packed together with no space between them.

                                  edit 2
                                  mrjj, I am guessing that you are using some kind of Designer Tool, can you please share the code that's generated? Maybe I can take a look at it as a reference to understand what I am doing wrong...

                                  Specifically:

                                  and even when commenting all size-related lines in the Bit constructor

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #19

                                  @Absurd

                                  #include <QGridLayout>
                                  #include <QLineEdit>
                                  #include <QSpacerItem>
                                  
                                  Widget::Widget(QWidget *parent)
                                      : QWidget(parent)
                                  {
                                      QGridLayout *gLay = new QGridLayout(this);
                                      gLay->setSpacing(0);
                                      gLay->setMargin(0);
                                  
                                      for(int i(0); i < 4; i++){
                                          for(int j(0); j < 32; j++) {
                                              if( j == 0 || j == 31){
                                                  gLay->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Minimum),i,j);
                                              } else {
                                                  QLineEdit *le = new QLineEdit(this);
                                                  le->setMaxLength(1);
                                                  le->setFixedWidth(16);
                                                  le->setAlignment(Qt::AlignCenter);
                                                  le->setText("0");
                                                  gLay->addWidget(le,i,j,1,1, Qt::AlignCenter);
                                              }
                                          }
                                      }
                                  }
                                  

                                  works fine for me:

                                  0_1545038347999_e912c7fe-8088-4e3a-a8ca-1fe85fca4538-image.png

                                  0_1545038365685_84906f62-49d6-4ce4-b3e2-f5bde4ebcaaf-image.png


                                  ups, j should run to 34 so that there are 32 QLineEdits.


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  1 Reply Last reply
                                  2
                                  • A Offline
                                    A Offline
                                    Absurd
                                    wrote on last edited by
                                    #20

                                    Thanks for the help. Appreciate it!

                                    Ok, running the exact same code as yours, I get different results...

                                    main.cpp:

                                    #include <QApplication>
                                    #include "Widget.h"
                                    
                                    
                                    
                                    int main(int argc, char* argv[]) {
                                        QApplication app(argc, argv);
                                    
                                        Widget widget;
                                        widget.show();
                                    
                                        return app.exec();
                                    }
                                    

                                    Widget.h:

                                    #ifndef WIDGET_H_
                                    #define WIDGET_H_
                                    
                                    #include <QWidget>
                                    
                                    class Widget : public QWidget {
                                        Q_OBJECT
                                    
                                    public:
                                        Widget(QWidget* parent = 0);
                                    };
                                    
                                    #endif
                                    

                                    Widget.cpp:

                                    #include "Widget.h"
                                    #include <QGridLayout>
                                    #include <QLineEdit>
                                    #include <QSpacerItem>
                                    
                                    Widget::Widget(QWidget *parent)
                                        : QWidget(parent)
                                    {
                                        QGridLayout *gLay = new QGridLayout(this);
                                        gLay->setSpacing(0);
                                        gLay->setMargin(0);
                                    
                                        for(int i(0); i < 4; i++){
                                            for(int j(0); j < 32; j++) {
                                                if( j == 0 || j == 31){
                                                    gLay->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Minimum),i,j);
                                                } else {
                                                    QLineEdit *le = new QLineEdit(this);
                                                    le->setMaxLength(1);
                                                    le->setFixedWidth(16);
                                                    le->setAlignment(Qt::AlignCenter);
                                                    le->setText("0");
                                                    gLay->addWidget(le,i,j,1,1, Qt::AlignCenter);
                                                }
                                            }
                                        }
                                    }
                                    

                                    And then:

                                    $> qmake-qt4 -project
                                    $> qmake-qt4 testing.pro
                                    $> make
                                    $> ./testing
                                    

                                    and the result:
                                    0_1545131067077_61efcb2e-9c97-48ec-8d01-eaaaa3fac713-image.png


                                    Maybe I am using different qt version?
                                    Maybe it's an environment issue?
                                    What else could it be?

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      Absurd
                                      wrote on last edited by Absurd
                                      #21

                                      Important update
                                      I tried to compile & run the same code on another machine, and guess what...

                                      0_1545142446417_9270b473-9f2b-43a9-b213-846dae79abd5-image.png

                                      $> uname -a
                                      Linux 4.15.0-42-generic #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
                                      

                                      Does anyone has an idea on why would different machine render the GUI differently?

                                      mrjjM 1 Reply Last reply
                                      0
                                      • A Absurd

                                        Important update
                                        I tried to compile & run the same code on another machine, and guess what...

                                        0_1545142446417_9270b473-9f2b-43a9-b213-846dae79abd5-image.png

                                        $> uname -a
                                        Linux 4.15.0-42-generic #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
                                        

                                        Does anyone has an idea on why would different machine render the GUI differently?

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #22

                                        @Absurd
                                        Are the both linuxes ? ( with GNOME)

                                        A 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @Absurd
                                          Are the both linuxes ? ( with GNOME)

                                          A Offline
                                          A Offline
                                          Absurd
                                          wrote on last edited by Absurd
                                          #23

                                          @mrjj
                                          Yes, they are both Linux-based, but not both are GNOME:

                                          The bad:

                                          $> uname -a 
                                          Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
                                          $> ls /usr/bin/*session
                                          /usr/bin/gnome-session*  /usr/bin/gnome-session-custom-session*
                                          $> qmake-qt4 --version
                                          QMake version 2.01a
                                          Using Qt version 4.8.5 in /usr/lib64
                                          

                                          The good:

                                          $> uname -a
                                          Linux 4.15.0-42-generic #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
                                          $> ls /usr/bin/*session
                                          /usr/bin/byobu-select-session*  /usr/bin/dbus-run-session*
                                          $> qmake-qt4 --version
                                          QMake version 2.01a
                                          Using Qt version 4.8.7 in /usr/lib/x86_64-linux-gnu
                                          
                                          mrjjM 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