Reputation: 1322
I am trying out QT to rework a WPF project. I want to use a QGridLayout for laying out buttons, each 64x64 px in dimension. However, the grid does not seem to respect these dimensions, I'm sure 64x64 is not that small. Also, there is a huge gap between the elements. How could I more tightly pack these buttons and make them all be uniformally 64 by 64 px? Preferrably even limit column and row count to 4 and window_height/64
Code:
#include "productbuttongrid.h"
#include <QPushButton>
ProductButtonGrid::ProductButtonGrid(QWidget *parent): QGridLayout(parent)
{
QPushButton *button = new QPushButton("Toode", parent);
button->setFixedSize(64,64);
QPushButton *button2 = new QPushButton("Toode2", parent);
button2->setFixedSize(64,64);
QPushButton *button3 = new QPushButton("Toode2", parent);
button3->setFixedSize(64,64);
QPushButton *button4 = new QPushButton("Toode2", parent);
button4->setFixedSize(64,64);
QPushButton *button5 = new QPushButton("Toode2", parent);
button5->setFixedSize(64,64);
this->addWidget(button,0,0);
this->addWidget(button2,0,1);
this->addWidget(button3,0,2);
this->addWidget(button4,0,3);
this->addWidget(button5,1,0);
}
And what this outputs, instead of the 64x64 buttons: They also seem a bit too small to be 64x64
Upvotes: 0
Views: 500
Reputation: 12879
If you want the buttons tightly spaced then you need to provide the QGridLayout
with some means of using any extra space it's given.
The easiest way in this instance would simply be to create a dummy row and column below and to the right of the buttons and allow them to stretch. So, simply add...
setRowStretch(rowCount(), 1);
setColumnStretch(columnCount(), 1);
at the end of your ProductButtonGrid
constructor.
If you want to have the button grid aligned centrally then arrange to have stretchable `extra' rows/columns above, below and left, right of the buttons.
You might also wish to remove the usual inter-item spacing using QGridLayout::setSpacing
.
Upvotes: 2