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
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).
Re: How to solve the compile error when using QLabel to display pictures in QMainWind
Quote:
Originally Posted by
Radek
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?
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.
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.