May 14, 2011

jk_mk jk_mk
Lab Rat
108 posts

displaying image using label in Qt4.7

 

Hello,

I am a beginner in developing with Qt and I am trying to create my first application. I want to create a window with a button and a label, in order to use a pixmap and finally display an image on the label. When I run my project I do not get any errors, but I do not see my image in my form either. I hope someone could help me and give me a solution. I have created the following code.

Thanks in advance

/////////// open_image.h /////////////

  1. #ifndef OPEN_IMAGE_H
  2. #define OPEN_IMAGE_H
  3.  
  4. #include <QtGui/QMainWindow>
  5. #include "ui_open_image.h"
  6.  
  7. class open_image : public QMainWindow, private Ui::open_imageClass
  8. {
  9.  Q_OBJECT
  10.  
  11. public:
  12.  open_image(QWidget *parent = 0, Qt::WFlags flags = 0);
  13.  ~open_image();
  14.  
  15. private:
  16.  Ui::open_imageClass ui;
  17.  
  18.  private slots:
  19.    void doThingA(void);
  20. };
  21.  
  22. #endif // OPEN_IMAGE_H

////////////// open_image.cpp ////

  1. #include "open_image.h"
  2.  
  3. open_image::open_image(QWidget *parent, Qt::WFlags flags)
  4.  : QMainWindow(parent, flags)
  5. {
  6.  setupUi(this);
  7.  QObject::connect(button, SIGNAL(triggered()), this, SLOT(doThingA()));
  8. }
  9.  
  10. open_image::~open_image()
  11. {
  12.  
  13. }
  14.  
  15. void open_image::doThingA(void)
  16. {
  17.  
  18.  label->setPixmap(QPixmap(":/open_image/C:/Users/manolis/Desktop/data/test.png", 0, Qt::AutoColor));
  19.  
  20. }

/////////// main.cpp //////

  1. #include "open_image.h"
  2. #include <QtGui/QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.  QApplication a(argc, argv);
  7.  open_image w;
  8.  w.show();
  9.  return a.exec();
  10. }

Mark up code, Tobias

5 replies

May 14, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3149 posts

The path in line 18 of open_image.cpp looks very fishy to me.

May 14, 2011

jk_mk jk_mk
Lab Rat
108 posts

What do you mean? I also tested the followings but with no results again. Do you thing it is a too long directory? Or there is a syntax error?

label->setPixmap(QPixmap(”:/Users/manolis/Desktop/data/test.png”, 0, Qt::AutoColor)); label->setPixmap(QPixmap(”:/open_image/C:/Users/manolis/Desktop/data/test.png”)); label->setPixmap(QPixmap(“C:/open_image/C:/Users/manolis/Desktop/data/test.png”, 0, Qt::AutoColor));

May 14, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi jk_mk,

where is your image located? it it’s in the resources, use the “:/xxx” notation, it it’s in the file system, use the path without that, like here:

  1. label->setPixmap(QPixmap(“C:/Users/manolis/Desktop/data/test.png, 0, Qt::AutoColor));

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 14, 2011

jk_mk jk_mk
Lab Rat
108 posts

It is in the resources (.qrc file), with URL, :/open_image/C:/Users/manolis/Desktop/data/test.png. I have typed this

  1. label->setPixmap(QPixmap(”:/open_image/C:/Users/manolis/Desktop/data/test.png”, 0, Qt::AutoColor));
  2. label->setPixmap(QPixmap(“C:/Users/manolis/Desktop/data/test.png”, 0, Qt::AutoColor));
  3. label->setPixmap(QPixmap(”:/C:/Users/manolis/Desktop/data/test.png”, 0, Qt::AutoColor));
  4. label->setPixmap(QPixmap(”:/Users/manolis/Desktop/data/test.png”, 0, Qt::AutoColor));

but I did not get anything

May 14, 2011

jk_mk jk_mk
Lab Rat
108 posts

Thanks for yours response. I just found my mistake. I should have put clicked() instead of triggered().

Thanks a lot

 
  ‹‹ Function pointers in Qt      Google C++ Style Guide ››

You must log in to post a reply. Not a member yet? Register here!