【发布时间】:2011-03-11 11:03:49
【问题描述】:
我需要在具有不同布局的其他小部件中设置小部件的方法...
这就像我们将小部件按一个布局分为带有标签的两部分,并且 这个小部件内部还有其他小部件,其布局类似于附加图像
而我们只有 4 个小部件:主小部件、标签 1 小部件、标签 2 小部件、按钮小部件,并且对于按钮使用一垂直和两个水平拉伸
有人能指出我正确的方法吗?谢谢。
【问题讨论】:
我需要在具有不同布局的其他小部件中设置小部件的方法...
这就像我们将小部件按一个布局分为带有标签的两部分,并且 这个小部件内部还有其他小部件,其布局类似于附加图像
而我们只有 4 个小部件:主小部件、标签 1 小部件、标签 2 小部件、按钮小部件,并且对于按钮使用一垂直和两个水平拉伸
有人能指出我正确的方法吗?谢谢。
【问题讨论】:
创建 QVBoxLayout,然后在其中添加两个 QHBoxLayout。在顶部 QHBoxLayout 添加标签,在底部添加拉伸、按钮、拉伸。
#include <QString>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLocale>
int main(int argc, char** argv){
QApplication app(argc, argv);
QWidget widget;
QVBoxLayout* vLayout = new QVBoxLayout(&widget);
QHBoxLayout* topLayout = new QHBoxLayout();
QHBoxLayout* bottomLayout = new QHBoxLayout();
QLabel* label1 = new QLabel(QObject::tr("Label1"));
QLabel* label2 = new QLabel(QObject::tr("Label2"));
label1->setAlignment(Qt::AlignCenter);
label2->setAlignment(Qt::AlignCenter);
QPushButton* btn1 = new QPushButton(QObject::tr("The Button!!!!"));
topLayout->addWidget(label1);
topLayout->addWidget(label2);
bottomLayout->addStretch();
bottomLayout->addWidget(btn1);
bottomLayout->addStretch();
vLayout->addLayout(topLayout);
vLayout->addLayout(bottomLayout);
widget.show();
return app.exec();
}
【讨论】: