给界面添加控件时,如果用QGridLayout会从正中间开始排列,如果要从左上角开始,可以利用一些巧解。
按如下界面设计,在添加控件时,将控件添加到frame里,然后给frame设置QGridLayout布局,这样看起来就像是从左上角开始排列。

代码示例:
QPushButton *b1 = new QPushButton;
b1->setFixedSize(100, 100);
QPushButton *b2 = new QPushButton;
b2->setFixedSize(100, 100);
QGridLayout *imageLayout = new QGridLayout;
imageLayout->setHorizontalSpacing(110);
imageLayout->setVerticalSpacing(60);
imageLayout->addWidget(b1, 0, 0);
imageLayout->addWidget(b2, 0, 1);
ui->frame->setLayout(imageLayout);
运行结果:

在Qt界面设计中,若使用QGridLayout会默认从中间开始排列控件。为实现从左上角开始排列,可以将控件添加到一个frame中,然后给frame设置QGridLayout布局。例如,创建两个QPushButton,设置它们的大小,并在frame的QGridLayout中添加,通过设置水平和垂直间距调整布局。
3451

被折叠的 条评论
为什么被折叠?



