rowspan of QGridLayout not working as expected
-
14-07-2021 - |
题
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:
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:
解决方案
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.
其他提示
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)
不隶属于 StackOverflow