Results 1 to 5 of 5

Thread: How to solve the compile error when using QLabel to display pictures in QMainWindow

  1. #1
    Join Date
    Nov 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question How to solve the compile error when using QLabel to display pictures in QMainWindow

    I'm in trouble to display a picture in QMainWindow using QLabel. I even copied the code from tutorial, but the compiler still fails to compile it. It always remind me a QLabel member is not defined.

    Here's my code :
    -----------------------------------------------------------------
    Error Information:
    Ui::WindowClass has no member "imageLabel"
    ---------------------------------------------------------------------
    /*Window.h*/
    #include <QtWidgets/QMainWindow>
    #include <QtWidgets/QLabel>
    #include "ui_window.h"

    class Window : public QMainWindow {
    Q_OBJECT
    public:
    Window(QWidget *parent = 0) {}
    private:
    Ui::WindowClass *ui;
    QLabel *imageLabel;
    };
    ---------------------------------------------------------------
    /*Window.cpp*/
    #include "window.h"
    #include "ui_window.h"

    Window::Window(QWidget *parent) : QMainWindow(parent), ui(new Ui::WindowClass) {
    ui->setupUi(this);
    QImage image("./Resources/Tree.png");
    ui->imageLabel->setPixmap(QPixmap::fromImage(image));
    }
    -----------------------------------------------------------------
    I know the imageLabel member is declared in Window, not in Ui::WindowClass, but how can I fix it?
    BTW, I use Qt5 and VS2013

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to solve the compile error when using QLabel to display pictures in QMainWind

    Check the .ui file. The label imageLabel you want to set does not exist in the UI. Redo and remove the QLabel *imageLablel; from the header file. You can also remove the QLabel header (it will be included from ui_window.h).

  3. #3
    Join Date
    Nov 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to solve the compile error when using QLabel to display pictures in QMainWind

    Quote Originally Posted by Radek View Post
    Check the .ui file. The label imageLabel you want to set does not exist in the UI. Redo and remove the QLabel *imageLablel; from the header file. You can also remove the QLabel header (it will be included from ui_window.h).
    Do you mean adding QLabel *imageLabel to ui_window.h file? I think .ui file is edited in Qt Creator Designer. So I added QLabel *imageLabel to ui_window.h file and deleted it in window.h file. The program can pass the compile now, but it doesn't show a window as I expected. Do you know what the problem is?

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to solve the compile error when using QLabel to display pictures in QMainWind

    No. Do not edit .ui files yourself (unless you are fixing Designer's errors). Remove the changes done by yourself, open the .ui. file in the Designer, and check the label. It has a different name, it is not a QLabel, or it is not in the .ui file at all. Fix the label, save, and try again.

  5. #5
    Join Date
    Nov 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to solve the compile error when using QLabel to display pictures in QMainWind

    Thank you, you answer helps a lot! I added a label in .ui file, using Designer. Also, I found in ui_window.h, there's a declaration: QLabel *label; so I gave up imageLabel and used label instead. Now I can open the window and show the picture.

Similar Threads

  1. How to solve this error
    By shivendra46d in forum Newbie
    Replies: 5
    Last Post: 16th January 2014, 12:42
  2. What is this error and how to solve it?
    By satya@sipl in forum Newbie
    Replies: 3
    Last Post: 26th July 2012, 09:04
  3. Replies: 1
    Last Post: 29th September 2009, 20:44
  4. How to display 16 bit grayscale pictures?
    By reimer in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 20:55
  5. Replies: 22
    Last Post: 7th December 2007, 10:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.