setting an image on the form
In the constructor of my project's class say myclass.cpp
i wrote the following code:
#include "myclass.h"
#include "ui_myclass.h"
MyClass::MyClass(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyClass)
{
ui->setupUi(this);
QString imagePath="xyz1.jpeg";
QImage logo(imagePath);
}
The image doesn't get loade on the form as specified by QImage class documentation of QT SDK 4.6.2....
Whats wrong??? Can anyone please guide???
I even tried this.....
#include "myclass.h"
#include "ui_myclass.h"
MyClass::MyClass(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyClass)
{
ui->setupUi(this);
QString imagePath="xyz1.jpeg";
QImage logo;
logo.load(imagePath);
}
Do i have to add anything else to set the image on the form???
Re: setting an image on the form
Yes.
Read about the difference between QImage and QPixmap.
Re: setting an image on the form
The simplest way to display an image is to use a QLabel and a QPixmap:
According to Qt doc:
Quote:
QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen
Code:
int main(int argc, char** argv)
{
imageLabel.setPixmap(image);
label.show();
return app.exec();
}
Re: setting an image on the form
thanks guys, but this isn't working too...According to toutarrive, i made the following changes..
I drew a Label on the UI form and wrote as following....
#include "myclass.h"
#include "ui_myclass.h"
MyClass::MyClass(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyClass)
{
ui->setupUi(this);
QPixmap logo("xyz1.jpg");
ui->Label->setPixmap(logo);
}
Re: setting an image on the form
Where is the image located?
Try using the absolute path.
Also, you can test QPixmap::isNull() to know if the loading of the image was successful.
You might want to read about using resources in Qt:
http://doc.trolltech.com/4.6/resources.html
Re: setting an image on the form
Hi, high_flyer, the image is located in the same folder where the project files are stored.
My imageExample folder consists of following files:
1)imageExample.pro
2)imageExample.cpp
3)imageExample.h
4)imageExample.ui
5)main.cpp
6)Logo.jpg
7)ui_imageExample.h
8)Makefile
9)Makefile.debug
10)Makefile.release
11)imageExample.pro.user
and two folders debug and release...
debug contains 5 files and release is empty
Re: setting an image on the form
put the image file where the exe is.