Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Display an image in a c++ program
Qt 6.11 is out! See what's new in the release blog

Display an image in a c++ program

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
12 Posts 4 Posters 453 Views 2 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
    JYG61
    wrote last edited by
    #1

    Hello,
    I just started to use qt for programming in C++, i tried some classical widgets (edit, buttons etc..) but i need to display an image on the main window, i found an code exemple
    QLabel *labelImage = new QLabel();
    QImage image("tonImage.jpg");
    labelImage->setPixmap(QPixmap::fromImage(image));
    that i added atfer
    ui->setupUi(this);
    in my MainWindow creator
    it just display an empty window, i checked using the isNull function of the QImage the check that my program find the jpeg file and i don't know why he does not display it.
    I had a quick look at the documentation about QImage and QLabel, there are hundreds of functions, just the syntax is described but i found no clear information about that each function does.
    Thanks to help me
    JY Giroud

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by SGaist
      #2

      Hi and welcome to devnet,

      labelImage is a new widget on which you didn't set a parent nor did you add to any layout that you may have in you ui file.
      If you already have a QLabel through the ui object, then call setPixmap on that label and remove labelImage.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JYG61
        wrote last edited by
        #3

        Thanks for your answer,
        I tried to add a label from the widget box, i added a widget called 'label' but it added a 'text label' what is the name of the widget i need to add from the widget box ?

        SGaistS 1 Reply Last reply
        0
        • J Offline
          J Offline
          JYG61
          wrote last edited by
          #4

          Hello,
          I did my first tests in C++ with QT using a free online training and i don't remember where i found it, is there anybody that can help me to find it again ?
          Thanks

          1 Reply Last reply
          0
          • J JYG61

            Thanks for your answer,
            I tried to add a label from the widget box, i added a widget called 'label' but it added a 'text label' what is the name of the widget i need to add from the widget box ?

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote last edited by
            #5

            @JYG61 said in Display an image in a c++ program:

            Thanks for your answer,
            I tried to add a label from the widget box, i added a widget called 'label' but it added a 'text label' what is the name of the widget i need to add from the widget box ?

            The name is a bit misleading but that is the correct widget.

            In your code you can simply do:

            setupUi(this):
            QImage image("tonImage.jpg");
            ui->label->setPixmap(QPixmap::fromImage(image));
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • J Offline
              J Offline
              JYG61
              wrote last edited by
              #6

              It works fine, thanks a lot but my problem is a bit more complicated, i need to load the QImage object directly from a memory buffer that contain the RGB value (8 bit per sample) of every pixel, do you know which method i should use to do this ?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote last edited by
                #7

                Look at this constructor using the QImage::Format_RGB888 image format.

                Please take the time to read the documentation, especially the part about the buffer lifetime.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • J Offline
                  J Offline
                  JYG61
                  wrote last edited by
                  #8

                  Hello,
                  I tried this code :
                  unsigned char tab[400];
                  int i;
                  for (i=0;i<400;i++) tab[i]=i%256;
                  ui->setupUi(this);
                  //QImage image("/home/x/passeport.jpg");
                  QImage image (tab, 100, 100, QImage::Format_RGB888, nullptr, nullptr);
                  ui->label->setPixmap(QPixmap::fromImage(image));
                  I just replaced le loading of the image file (commented) by an unsigned char[400], there are no errors at compilation but when i try to start it, it crashes immediately

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • J JYG61

                    Hello,
                    I tried this code :
                    unsigned char tab[400];
                    int i;
                    for (i=0;i<400;i++) tab[i]=i%256;
                    ui->setupUi(this);
                    //QImage image("/home/x/passeport.jpg");
                    QImage image (tab, 100, 100, QImage::Format_RGB888, nullptr, nullptr);
                    ui->label->setPixmap(QPixmap::fromImage(image));
                    I just replaced le loading of the image file (commented) by an unsigned char[400], there are no errors at compilation but when i try to start it, it crashes immediately

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote last edited by
                    #9

                    @JYG61 said in Display an image in a c++ program:

                    it crashes immediately

                    What do you expect?
                    An rgb image with 100x100 needs a little bit more memory than 400bytes... 100 * 100 * 3 I would guess.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    3
                    • J Offline
                      J Offline
                      JYG61
                      wrote last edited by
                      #10

                      Oups
                      Sorry to disturb with that stupid question, i just missed a factor 100 (as it seems to be 4 bytes aligned), i just added two zeros and it works, of course i need to put a real image in my memory bloc but that is nt a problem.
                      Thanks

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JYG61
                        wrote last edited by
                        #11

                        Is there a way to mark this topic solved ?

                        jsulmJ 1 Reply Last reply
                        0
                        • J JYG61

                          Is there a way to mark this topic solved ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote last edited by
                          #12

                          @JYG61 See "Topics Tools" at the top

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • J JYG61 has marked this topic as solved

                          • Login

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