Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Displaying image using label in Qt4.7
Forum Updated to NodeBB v4.3 + New Features

Displaying image using label in Qt4.7

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 3 Posters 14.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jk_mk
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jk_mk
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jk_mk
              wrote on last edited by
              #6

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

              Thanks a lot

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved