Minimizing Vertical Spacing in QGridLayout
-
Hi and welcome to the forums
If i set both spacing ( horz and vert ) to zero there seems to be no spaces ?
Did you set some fix size on the LineEdits ?
-
Het mrjj, thanks for the help.
Indeed I did:QFont font("Courier New", 10, QFont::Bold); setFont(font); setMaxLength(1); // I want to limit the box to one digit setFrame(false); setFixedWidth(16); // I want it to be small setAlignment(Qt::AlignCenter);
Why?
How does the fix width effect the width around the text box?edit
removing the setFixedWidth(16); did not solve the problem, but now I have a wider box, which is undesirable:QFont font("Courier New", 10, QFont::Bold); setFont(font); setMaxLength(1); setFrame(false); //setFixedWidth(16); setAlignment(Qt::AlignCenter);
Produces:
-
Hi,
layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0);
should do what you want.
-
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 fromQLineEdit
) 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:
Are you sure that the margin I see is not a margin that's being claimed by the
QLineEdit
(rather than theQGridLayout
)?I can upload the full code if needed, although there's not much more than the above...
Thanks.
-
What desktop environment are you using ?
-
@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.
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.
-
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"); }
I am still left with this:
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... -
Hi
Yes, it the one build into Creator.
Anyway, i fiddled around with it and came up withQLineEdit *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.
test project
https://www.dropbox.com/s/la5qrlz48nlwr9p/TonsOfEdit.zip?dl=0 -
:(
I changed myBit
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
QFont
s fault), but still the same: -
@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.
-
@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"); }
I am still left with this:
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
and the result:
Maybe I am using different qt version?
Maybe it's an environment issue?
What else could it be? -
Important update
I tried to compile & run the same code on another machine, and guess what...$> 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?