3

I am new to Qt and C++ and working on an application and I am trying to add QLabel in a QWidget, using QHBoxLayout. 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.

Marc Dirven
  • 309
  • 2
  • 18
Sadaab
  • 83
  • 1
  • 1
  • 7
  • 2
    1) You have a typo in your code - should be not `QLable`, but `QLabel`. 2) Did you try it with the most simple project, with just this code, nothing else? I've just tried it and it works just fine for me. – Googie Oct 10 '14 at 13:52
  • Sorry for the typo...It seems like the layout is not working in my project.So i am making an instance of Qlabel inside the parent class and setting its position and size using setsize() and move () methods respectively. Its working well now. – Sadaab Oct 14 '14 at 08:56

1 Answers1

9

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

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

which I assume it's what you want...

Iuliu
  • 4,001
  • 19
  • 31
  • Sorry for the typo...It seems like the layout is not working in my project.So i am making an instance of Qlabel inside the parent class and setting its position and size using setsize() and move () methods respectively. Its working well now. – Sadaab Oct 14 '14 at 08:56