titicplusplus
titicplusplus

Reputation: 49

QWidgets from QGridLayout that don't take up all the space

I try to create a responsive QGridLayout with Qt5. Here is the part of my function that initializes the QGridLayout.

std::vector< std::vector< QPushButton* > > tab2d;
// [...]
for (int i = yyy-1; i >= 0; i--)
{
    for (int j {0}; j < xxx; j++)
    {
        if (tab2d[j][i] == nullptr) 
        {
            tab2d[j][i] = new QPushButton(this);
            tab2d[j][i]->setIcon(icon);
            tab2d[j][i]->setIconSize(QSize(150,150)); //);
            tab2d[j][i]->setStyleSheet("background: none");
            
            connect(tab2d[j][i], &QPushButton::clicked, this, [this , i, j]{ createNewSlide(j,i); });

        }
        else
        {
            connect(tab2d[j][i], &QPushButton::clicked, this, [this , i, j]{ button_c(j,i); });
        }
        
        grid->addWidget(tab2d[j][i],yyy-1- i, j, 1, 1);
}

grid->setContentsMargins(0, 0, 0, 0);
grid->setSpacing(0);

But the result is bad:

The buttons are not the same size

The buttons are not the same size

When I resize the window:

When I resize the window

I would like to have the same size for all buttons, and that there is no spacing when I resize the window, I would like the QGridLayout to adapt the buttons to the new window size.

I tried something with the QGridLayout::setSizeConstraint with different values but it doesn't change anything.

So can you help me to find a solution. Thank you

Upvotes: 1

Views: 452

Answers (1)

titicplusplus
titicplusplus

Reputation: 49

Thank you again G.M.

The solution is to use setSizePolicy with

tab2d[j][i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

It's work

Upvotes: 2

Related Questions