PDA

View Full Version : setting an image on the form



Yayati.Ekbote
15th March 2010, 08:01
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???

high_flyer
15th March 2010, 08:34
Yes.
Read about the difference between QImage and QPixmap.

toutarrive
15th March 2010, 08:37
The simplest way to display an image is to use a QLabel and a QPixmap:
According to Qt doc:

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



int main(int argc, char** argv)
{
QApplication app(argc, argv);
QPixmap image("image.jpg");
QLabel imageLabel();
imageLabel.setPixmap(image);
label.show();
return app.exec();
}

Yayati.Ekbote
15th March 2010, 10:34
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);
}

high_flyer
15th March 2010, 10:47
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

Yayati.Ekbote
15th March 2010, 11:01
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

high_flyer
15th March 2010, 11:39
put the image file where the exe is.