c++ - 在 Qt 上添加小部件并将它们排列到屏幕的一角

标签 c++ qt user-interface layout qt5

我尝试在屏幕右上角排列一些小部件,其中包括两个标签和一个图像。我正在使用 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

enter image description here

现在我还没有在我的代码中添加左侧标签。提前致谢:)

最佳答案

尝试这样的布局:

#include "screen.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QBitmap>
#include <QFrame>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    auto *widget = new QWidget(this);
    auto *frmLogin = new QFrame(this);
    auto *layoutMain = new QVBoxLayout(widget);
    auto *layoutTitle = new QHBoxLayout();
    auto *layoutLogin = new QHBoxLayout(frmLogin);
    auto *layoutInfo = new QVBoxLayout();
    auto *labTitle = new QLabel(tr("Some other stuff you want in the title"), this);
    auto *labBody = new QLabel(tr("Some other stuff you want in the body"), this);
    auto *labAvatar = new QLabel(this);
    auto *labUser = new QLabel(tr("User"), this);
    auto *labValue = new QLabel(tr("Value"), this);
    QPixmap p(":/pix/images/avatars/user.png");

    labTitle->setAlignment(Qt::AlignCenter);
    labTitle->setStyleSheet(".QLabel { background-color: white; }");

    labBody->setAlignment(Qt::AlignCenter);

    labAvatar->setPixmap(p);
    labAvatar->setMask(p.mask());

    layoutInfo->addWidget(labUser);
    layoutInfo->addWidget(labValue);

    layoutLogin->addWidget(labAvatar);
    layoutLogin->addLayout(layoutInfo);

    frmLogin->setStyleSheet(".QFrame { background-color : orange; }");
    frmLogin->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    layoutTitle->addWidget(labTitle);
    layoutTitle->addWidget(frmLogin);

    layoutMain->addLayout(layoutTitle);
    layoutMain->addWidget(labBody);
    layoutMain->setContentsMargins(0, 0, 0, 0);
    layoutMain->setSpacing(0);

    setCentralWidget(widget);
    showFullScreen();
}

上面的代码产生以下布局:

enter image description here

关于c++ - 在 Qt 上添加小部件并将它们排列到屏幕的一角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633994/

相关文章:

C++ 嵌套模板 : inaccessible static method

c++ - 如何在 C++ 中正确分配 char*?

c++ - 如何在 Qhash 中使用 Qmap?

JavaFX - 使 ScrollPane 自动滚动

当我将它放入它自己的方法中时,Java gui 没有显示

c++ - 如何知道库是调试库还是发布?

c++ - 在 C++ 中,当在控制结构中使用时,是什么导致赋值计算为真或假?

java - 将 Vaadin Designer 连接到 Java 代码

c++ - 启动 URL - 跨平台 (C++)

c++ - QRubberBand,如何在上面画画