Minimizing Vertical Spacing in QGridLayout
-
@Absurd
The spacer item is the key. else it will distribute the remaining space among the lineEdits.
its how layout works. -
But that's the thing; why is there remaining space in the first place, if I used QGridLayout.setSpacing(0)?
-
@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? -
@J-Hilk see what I wrote above:
@Absurd said in Minimizing Vertical Spacing in QGridLayout:
I did nothing special on the
QGridLayout
nor on theQLineEdit
...
You can see the constructor for the main Widget above (PacketHeader
), and even when commenting all size-related lines in theBit
constructor (which is actually aQLineEdit
):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"); }
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 -
#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:
ups, j should run to 34 so that there are 32 QLineEdits.
-
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
Maybe I am using different qt version?
Maybe it's an environment issue?
What else could it be? -
-
@Absurd
Are the both linuxes ? ( with GNOME) -
@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
-
@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 ?
-
Adding the code you suggested;
The Bad prints out "oxygen",
The Good prints out "windows".What does that mean?
Thanks.
-
@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 ?
-
You nailed it!
I added this code on the 'Good', butQStyleFactory::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); }
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 withsudo apt install qt4-default
)Thanks again!
-
@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. - How is the QStyle determined inside a QApplication?
-
I will try installing 4.8.7 and will update if the behavior is different there...
23/29