Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. loading image file into label
Forum Updated to NodeBB v4.3 + New Features

loading image file into label

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.6k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    The path is wrong
    When the program is run, the current folder is the build folder
    and not the project folder so it won't be able to find the "123.jpeg" in the project folder.

    The easy way is to include a resource file to the project and then add the image to the resource file
    and then use the correct syntax

    QImageReader read(":/123.jpeg");
    notice the :/ in front

    N 1 Reply Last reply
    6
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #3

      to add to @mrjj
      here are further information about Qt's resource system

      https://doc.qt.io/qt-5/resources.html

      also make sure to rerun qmake after adding a new resource file and/or resource to your resource file


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        The path is wrong
        When the program is run, the current folder is the build folder
        and not the project folder so it won't be able to find the "123.jpeg" in the project folder.

        The easy way is to include a resource file to the project and then add the image to the resource file
        and then use the correct syntax

        QImageReader read(":/123.jpeg");
        notice the :/ in front

        N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by
        #4

        @mrjj

        thnx, that worked.

            QPixmap pixmap;
            QImageReader read(":/123.jpeg");
            QImage image = read.read();
        
            if (!image.isNull()){
                QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
                ui->label_picture->pixmap(pixmap.fromImage(image));
            }
            else{
                QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
            }
        

        the image is loaded, but ...

        ui->label_picture->pixmap(pixmap.fromImage(image));
        

        results in:

        error: no matching function for call to 'QLabel::pixmap(QPixmap)'
                 ui->label_picture->pixmap(pixmap.fromImage(image));
                                                                  ^
        

        i also tried:

        ui->label_picture->pixmap.load(image);
        

        but also no working:

        error: '((MainWindow*)this)->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::label_picture->QLabel::pixmap' does not have class type
                 ui->label_picture->pixmap.load(image);
                 ^
        
        J.HilkJ 1 Reply Last reply
        0
        • N Natural_Bugger

          @mrjj

          thnx, that worked.

              QPixmap pixmap;
              QImageReader read(":/123.jpeg");
              QImage image = read.read();
          
              if (!image.isNull()){
                  QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
                  ui->label_picture->pixmap(pixmap.fromImage(image));
              }
              else{
                  QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
              }
          

          the image is loaded, but ...

          ui->label_picture->pixmap(pixmap.fromImage(image));
          

          results in:

          error: no matching function for call to 'QLabel::pixmap(QPixmap)'
                   ui->label_picture->pixmap(pixmap.fromImage(image));
                                                                    ^
          

          i also tried:

          ui->label_picture->pixmap.load(image);
          

          but also no working:

          error: '((MainWindow*)this)->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::label_picture->QLabel::pixmap' does not have class type
                   ui->label_picture->pixmap.load(image);
                   ^
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #5

          @Natural_Bugger

          setPixmap is the setter function.

          ui->label_picture->setPixmap(Pixmap::fromImage(image));
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          N 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @Natural_Bugger

            setPixmap is the setter function.

            ui->label_picture->setPixmap(Pixmap::fromImage(image));
            
            N Offline
            N Offline
            Natural_Bugger
            wrote on last edited by
            #6

            @J-Hilk

            thnx for your help.

                    QPixmap pixmap;
                    QImageReader read(":/123.jpeg");
                    QImage image = read.read();
                    pixmap.fromImage(image);
                
                    if (!image.isNull()){
                        QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
            
                        ui->label_picture->setPixmap(pixmap);
                    }
                    else{
                        QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
                    }
            

            QMessageBox = "is loaded"
            but still doesn't show up in the label / user interface, although it compiles without errors and executes.

            J.HilkJ 1 Reply Last reply
            0
            • N Natural_Bugger

              @J-Hilk

              thnx for your help.

                      QPixmap pixmap;
                      QImageReader read(":/123.jpeg");
                      QImage image = read.read();
                      pixmap.fromImage(image);
                  
                      if (!image.isNull()){
                          QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
              
                          ui->label_picture->setPixmap(pixmap);
                      }
                      else{
                          QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
                      }
              

              QMessageBox = "is loaded"
              but still doesn't show up in the label / user interface, although it compiles without errors and executes.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @Natural_Bugger
              I deliberitly wrote Pixmap::fromImage(image) because fromPixmap returns a pixmap. It does not apply it on itself!


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              N 1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #8

                Hi
                Just as a note.
                Unless you really want QImage first and use QImageReader etc then
                this is also working

                ui->label_picture->setPixmap(QPixmap(":/123.jpeg"));
                Let the pixmap load it directly by self.

                1 Reply Last reply
                4
                • J.HilkJ J.Hilk

                  @Natural_Bugger
                  I deliberitly wrote Pixmap::fromImage(image) because fromPixmap returns a pixmap. It does not apply it on itself!

                  N Offline
                  N Offline
                  Natural_Bugger
                  wrote on last edited by
                  #9

                  @J-Hilk

                  i tried that, but ...

                  ui->label_picture->setPixmap(Pixmap::fromImage(image));
                  

                  results in:

                  error: 'Pixmap' has not been declared
                           ui->label_picture->setPixmap(Pixmap::fromImage(image));
                                                        ^
                  

                  i tried to solve it and i changed Pixmap into pixmap, which resulted in:

                  error: 'pixmap' is not a class, namespace, or enumeration
                           ui->label_picture->setPixmap(pixmap::fromImage(image));
                                                        ^
                  

                  i'm rusty

                  J.HilkJ 1 Reply Last reply
                  0
                  • N Natural_Bugger

                    @J-Hilk

                    i tried that, but ...

                    ui->label_picture->setPixmap(Pixmap::fromImage(image));
                    

                    results in:

                    error: 'Pixmap' has not been declared
                             ui->label_picture->setPixmap(Pixmap::fromImage(image));
                                                          ^
                    

                    i tried to solve it and i changed Pixmap into pixmap, which resulted in:

                    error: 'pixmap' is not a class, namespace, or enumeration
                             ui->label_picture->setPixmap(pixmap::fromImage(image));
                                                          ^
                    

                    i'm rusty

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #10

                    @Natural_Bugger
                    I'm terribly sorry, I think the auto correction removed the Qfrom QPixmap

                    ui->label_picture->setPixmap(QPixmap::fromImage(image));
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    3
                    • N Offline
                      N Offline
                      Natural_Bugger
                      wrote on last edited by
                      #11

                      @mrjj

                      thnx, that worked.

                      @J-Hilk

                      thnx, now it's working.

                      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