【发布时间】:2019-01-09 01:36:17
【问题描述】:
我尝试在屏幕右上角安排一些小部件,其中包括两个标签和一个图像。我正在使用 HBoxlayout。当我尝试在 HBoxlayout 中将小部件添加到屏幕时,所有小部件都没有排列在我希望它们放置的位置。那么如何将这些安排到屏幕顶部。这是我尝试过的代码
#include "screen.h"
#include "ui_screen.h"
#include<QGridLayout>
#include<QLabel>
#include<QBitmap>
screen::screen(QWidget *parent) :
QWidget(parent),
ui(new Ui::screen)
{
ui->setupUi(this);
QGridLayout *g=new QGridLayout();
QHBoxLayout *h=new QHBoxLayout();
QLabel *l=new QLabel();
QLabel *l2=new QLabel();
QLabel *l3=new QLabel();
QPixmap p(":/img/img/user.jpg");
l->setPixmap(p);
l->setMask(p.mask());
l->show();
l->setStyleSheet("QLabel { background-color : red; color : blue; }");
l2->setText("User");
l3->setText("Value");
h->addWidget(l,0,Qt::AlignRight);
h->addWidget(l2,0,Qt::AlignRight);
h->addWidget(l3,0,Qt::AlignRight);
g->addLayout(h,0,0,1,1,Qt::AlignTop);
this->setLayout(g);
this->showFullScreen();
}
screen::~screen()
{
delete ui;
}
我尝试制作的布局Sample layout
现在我还没有在我的代码中添加左侧标签。在此先感谢:)
【问题讨论】:
-
你为什么有
ui->setupUi(this);?当您在 Qt Designer 中(通过鼠标)创建 UI 时使用此选项。如果您在 Designer 中有一些布局设置,那么您的小部件已经有布局并且布局无法替换(Qt 记录了各自的争吵)。 -
其实我忘了删除这个。但我并没有得到最后一点,即如何制作这种结构。
-
看起来您的物品被拉长了。使用
addStretch,这样可用空间就会被拉伸而不是你的标签。
标签: c++ qt user-interface layout qt5