0

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

enter image description here

4
  • Note: you call button2->setFixedSize(64,64); four times. Probably not what you intended. Commented Jul 30, 2018 at 13:49
  • @G.M.correct. Edited the Q with the correct code, and new output. Commented Jul 30, 2018 at 14:05
  • @RandoHinn What is the problem with the output? I see that the buttons are the right size. Commented Jul 30, 2018 at 14:47
  • Spacing. I want a tight grid.. Commented Jul 30, 2018 at 15:03

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.