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 430 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
    #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