Adding a QLabel to a QWidget
29,907
Your code has a typo, it's QLabel
, not 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
#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
#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
which I assume it's what you want...
Author by
Sadaab
Updated on December 31, 2020Comments
-
Sadaab over 3 years
I am new to Qt and C++ and working on an application and I am trying to add
QLabel
in aQWidget
, usingQHBoxLayout
. 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.
The string
"Random String"
doesn't get displayed inside the label.Please help.