简体   繁体   English

将 QLabel 添加到 QWidget

[英]Adding a QLabel to a QWidget

I am new to Qt and C++ and working on an application and I am trying to add QLabel in a QWidget , using QHBoxLayout .我是 Qt 和 C++ 的新手,正在开发一个应用程序,我正在尝试使用QHBoxLayoutQWidget添加QLabel I am setting the text of label to something but it is not visible in the Label.我正在将标签文本设置为某些内容,但它在标签中不可见。

Here is the piece of the code:这是代码的一部分:

setStyleSheet( "QWidget{ background-color : rgba( 160, 160, 160, 255); border-radius : 7px;  }" );
QLabel *label = new QLabel(this);
QHBoxLayout *layout = new QHBoxLayout();
label->setText("Random String");
layout->addWidget(label);
setLayout(layout);    

The styleSheet is for the Widget in which QLabel is added. styleSheet 用于添加 QLabel 的 Widget。

The string "Random String" doesn't get displayed inside the label.字符串"Random String"不会显示在标签内。

Please help.请帮忙。

Your code has a typo, it's QLabel , not QLable ...您的代码有一个错字,它是QLabel而不是QLable ...

Assuming that this would notify you at compile time I don't see what is the problem with the code, maybe you could share more of your project with us...假设这会在编译时通知您,我看不出代码有什么问题,也许您可​​以与我们分享更多您的项目...

I did a small test of this class:我对这门课做了一个小测试:

mynewwidget.h mynewwidget.h

#ifndef MYNEWWIDGET_H
#define MYNEWWIDGET_H

#include <QWidget>

class MyNewWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyNewWidget(QWidget *parent = 0);
};

#endif // MYNEWWIDGET_H

mynewwidget.cpp mynewwidget.cpp

#include "mynewwidget.h"

#include <QHBoxLayout>
#include <QLabel>

MyNewWidget::MyNewWidget(QWidget *parent) :
    QWidget(parent)
{
    setStyleSheet( "QWidget{ background-color : rgba( 160, 160, 160, 255); border-radius : 7px;  }" );
    QLabel *label = new QLabel(this);
    QHBoxLayout *layout = new QHBoxLayout();
    label->setText("Random String");
    layout->addWidget(label);
    setLayout(layout);
}

And the result is结果是

http://i.imgur.com/G6OMHZX.png

which I assume it's what you want...我认为这是你想要的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM