Qt World Summit: Register Today!

QLabel does not name a type

  • Greetings all,
    I'm a total newbie in Qt. I'm trying to add a public attribute to my OMR Class (Main class). The idea is to use this publicly available attribute to access a programatically added QLabel. To do this I added the property in my H file, like this:
    #ifndef OMR_H
    #define OMR_H

    #include <QMainWindow>

    namespace Ui {
    class OMR;

    class OMR : public QMainWindow

    explicit OMR(QWidget *parent = 0);
    QLabel *label_img; // <<< This one

    private slots:
    void on_loadimg_clicked();

    void on_pushButton_clicked();

    Ui::OMR *ui;

    #endif // OMR_H
    But when I try to compile I get the error: "QLabel does not name a type"
    Why does that happen?

    PS: I'm pretty sure there is a better way to do what I'm trying to do, and if you can, please tell me how. But I also want to know exactly why is QT not allowing me to do it this way.

    Thanks in Advance.

  • Moderators

    You are missing #include <QLabel>

    It greatly depends on what you are trying to do but it's usually not a good idea to expose "naked pointers" to the ui element. Instead you can make this label private and add some public access methods that have more semantic meaning, eg. displayMessage(Qstring msg) or showCountOfSomething(int value) and then manipulate label directly inside of them.

    This is sorta general rule of encapsulation. Outside of main window you shouldn't be concerned with gui types or pointers, but call some well named methods that take care of internals.

  • Geez! that was really stupid! LOL! that fixed the problem. I did try to include QLabel but I did it only on the CPP's. Anyways thanks for your (fast) reply. I'll keep in mind your suggestion.

  • This post is deleted!

Log in to reply