Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rowspan of QGridLayout not working as expected

Here is how to draw a button that spans 2 columns:

#include <QtGui>

int main(int argv, char **args)
{
    QApplication app(argv, args);

    QPushButton *foo = new QPushButton("foo");
    QPushButton *bar = new QPushButton("bar");
    QPushButton *baz = new QPushButton("baz");

    QGridLayout *layout = new QGridLayout();
    layout->addWidget(foo, 0, 0);
    layout->addWidget(bar, 0, 1);
    layout->addWidget(baz, 1, 0, 1, 2); // span 2 columns

    QWidget window;
    window.setLayout(layout);
    window.setWindowTitle("test");
    window.show();

    return app.exec();
}

Running the code gives me:

enter image description here

If I change the layout in order to get a button, baz, that spans 2 rows I fail:

layout->addWidget(foo, 0, 0);
layout->addWidget(bar, 1, 0);
layout->addWidget(baz, 0, 1, 2, 1); // (try to) span 2 rows

Here is what I get:

enter image description here

like image 748
tshepang Avatar asked Dec 05 '25 19:12

tshepang


2 Answers

Your layout is fine, the baz button is spanning two rows. The problem is that it doesn't use all the available space. You have to change the vertical resize policy of your button from Fixed to MinimumExpanding.

like image 161
Vicent Avatar answered Dec 08 '25 13:12

Vicent


I added the following, after which all was well:

 foo->setSizePolicy(QSizePolicy::MinimumExpanding,
                    QSizePolicy::MinimumExpanding);
 bar->setSizePolicy(QSizePolicy::MinimumExpanding,
                    QSizePolicy::MinimumExpanding);
 baz->setSizePolicy(QSizePolicy::MinimumExpanding,
                    QSizePolicy::MinimumExpanding);

(thanks)

like image 26
tshepang Avatar answered Dec 08 '25 12:12

tshepang