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 /////////////
    @
    #ifndef OPEN_IMAGE_H
    #define OPEN_IMAGE_H

    #include <QtGui/QMainWindow>
    #include "ui_open_image.h"

    class open_image : public QMainWindow, private Ui::open_imageClass
    {
    Q_OBJECT

    public:
    open_image(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~open_image();

    private:
    Ui::open_imageClass ui;

    private slots:
    void doThingA(void);
    };

    #endif // OPEN_IMAGE_H
    @

    ////////////// open_image.cpp ////
    @
    #include "open_image.h"

    open_image::open_image(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    setupUi(this);
    QObject::connect(button, SIGNAL(triggered()), this, SLOT(doThingA()));
    }

    open_image::~open_image()
    {

    }

    void open_image::doThingA(void)
    {

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

    }
    @

    /////////// main.cpp //////
    @
    #include "open_image.h"
    #include <QtGui/QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    open_image w;
    w.show();
    return a.exec();
    }
    @

    [Mark up code, Tobias]


  • Moderators

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



  • 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));



  • 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:

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



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

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

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

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

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

    but I did not get anything



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

    Thanks a lot


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.