1
votes

I have a QGridLayout inside a center widget, which contains a supposedly fixed-sized widget (referred to as inner widget) that also has a QGridLayout filled with buttons. Inner widget's size is determined by how many buttons are there in the grid, and is supposed to be an exact fit (no spacing between the buttons, FixedSize policy applied in buttons' constructor), and all buttons have their sizes and policies set in the constructor. Now, if I don't put inner widget into a layout of any kind, it works just fine, I get nice square buttons. But if I put inner widget into a grid layout, all buttons suddenly change their sizes, and widget also doesn't seem like keeping its size. Why?

Edit: MyButtonTable:

MyButtonTable::MyButtonTable(QWidget *parent) : QWidget(parent), array()
{
    size_x = 2;
    size_y = 2;
    QGridLayout* layout = new QGridLayout();
    for(size_t x = 0; x < size_x; x++) {
        this->array.push_back(std::vector<MyRightClickButton*>());
    }
    for(size_t x = 0; x < size_x; x++) {
        for(size_t y = 0; y < size_y; y++) {
            this->array[x].push_back(new button_t());
            QObject::connect(array[x][y], SIGNAL(rightClicked()), this, SLOT(internalRightClick()));
            QObject::connect(array[x][y], SIGNAL(clicked()), this, SLOT(internalClick()));
            layout->addWidget(array[x][y], x, y);
        }
    }
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    this->setLayout(layout);
    this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    this->setMinimumSize(QSize(0,0));
    this->resize(QSize(10*size_y,10*size_x));
}

MyRightClickButton(QWidget *parent = 0):QPushButton(parent) {
        marked = false;
        this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        this->setMinimumSize(QSize(0,0));
        this->resize(QSize(10,10));
    }
1
Well, as far as I'm concerned, layouts have their own size policies and they ignore the fixed size of the widget. @RomaValcerAthena
@Athena and what can be done about it?RomaValcer
I should use either of them, as far as I've experienced.Athena
@Athena either of what?RomaValcer
either fixedSize or layout managerAthena

1 Answers

0
votes

A layout manager is used to arrange child widgets in them. The arrangement designates each layout manager. Layout managers adjust the size of their child widgets based on the resize policies. Even if you are setting a fixed size for a child widget, using resize() or setGeometry(), they will be resized by the layout manager, if you did not set the resize policy of the child widget.

For example

widget->setFixedSize (100, 100);
widget->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);

This is how it should be done.