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.5k 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.
  • 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
                            • A Absurd

                              @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 Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #24

                              @Absurd
                              Hi
                              Could yo try to do

                              #include <QDebug>
                              #include <QStyle>
                              
                              int main(int argc, char *argv[])
                              {
                                  QApplication a(argc, argv);
                                  qDebug() << QApplication::style()->objectName();
                              ...normal code...
                              

                              and see if they list different styles ?

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

                                Adding the code you suggested;
                                The Bad prints out "oxygen",
                                The Good prints out "windows".

                                What does that mean?

                                Thanks.

                                mrjjM 1 Reply Last reply
                                0
                                • A Absurd

                                  Adding the code you suggested;
                                  The Bad prints out "oxygen",
                                  The Good prints out "windows".

                                  What does that mean?

                                  Thanks.

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

                                  @Absurd
                                  It means that it uses different QStyles for the look and apparently
                                  the oxygen draws the LineEdit with little border or something like that.

                                  on the good, could you try

                                   QStyle *style = QStyleFactory::create("oxygen");
                                      if(style)
                                      {
                                          qDebug("Oxygen loaded!");        
                                          QApplication::setStyle(style);
                                      }
                                  

                                  and see if it then get a space like the bad one ?

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

                                    You nailed it!
                                    I added this code on the 'Good', but QStyleFactory::create("oxygen"); returned NULL for some reason (do you happen to know how can I inspect why? is there something like 'errno' I can print out?), so instead I tried this on the 'Bad':

                                    QStyle *style = QStyleFactory::create("windows");
                                       if(style)
                                       {
                                           qDebug("windows loaded!");        
                                           QApplication::setStyle(style);
                                       }
                                    

                                    and voilĂ :
                                    0_1545145924547_6f35a3ef-60b0-4f07-b136-60657b3ef691-image.png

                                    Now, I guess I can use it as a WA, but I'm curious:
                                    How is the QStyle determined inside a QApplication?
                                    Could it be related to the qt4 versions? (in the 'Bad' machine I didn't have to install qt4 - it was there when I got it, but on the 'Good' I had to install it with sudo apt install qt4-default)

                                    Thanks again!

                                    mrjjM 1 Reply Last reply
                                    0
                                    • A Absurd

                                      You nailed it!
                                      I added this code on the 'Good', but QStyleFactory::create("oxygen"); returned NULL for some reason (do you happen to know how can I inspect why? is there something like 'errno' I can print out?), so instead I tried this on the 'Bad':

                                      QStyle *style = QStyleFactory::create("windows");
                                         if(style)
                                         {
                                             qDebug("windows loaded!");        
                                             QApplication::setStyle(style);
                                         }
                                      

                                      and voilĂ :
                                      0_1545145924547_6f35a3ef-60b0-4f07-b136-60657b3ef691-image.png

                                      Now, I guess I can use it as a WA, but I'm curious:
                                      How is the QStyle determined inside a QApplication?
                                      Could it be related to the qt4 versions? (in the 'Bad' machine I didn't have to install qt4 - it was there when I got it, but on the 'Good' I had to install it with sudo apt install qt4-default)

                                      Thanks again!

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

                                      @Absurd said in Minimizing Vertical Spacing in QGridLayout:
                                      Hi
                                      As far as i know QStyleFactory cannot tell you why. only list available styles.

                                      • How is the QStyle determined inside a QApplication?
                                        Never saw the actual for that detection. so cant tell. sorry.

                                      Im wondering if 4.8.5 had a small bug where even disable the border would not
                                      reduce the space used. And that was fixed in 4.8.7. Or it simply is a QStyle drawing difference.

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

                                        I will try installing 4.8.7 and will update if the behavior is different there...

                                        1 Reply Last reply
                                        1

                                        • Login

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