下面是如何绘制一个跨越 2 列的按钮:
#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();
}
运行代码给了我:
如果我更改布局以获取一个baz
跨越 2 行的按钮,我将失败:
layout->addWidget(foo, 0, 0);
layout->addWidget(bar, 1, 0);
layout->addWidget(baz, 0, 1, 2, 1); // (try to) span 2 rows
这是我得到的: