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. How to display a picture

How to display a picture

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 8 Posters 85.2k Views
  • 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.
  • P Offline
    P Offline
    Payx
    wrote on 2 Oct 2016, 10:40 last edited by
    #18

    Thanks but how to return it ?

    ""ui->label_3->setText( QString("Size: %1, %2").arg(s.width()).arg(s.height()) );""

    there is not more simple ?

    M 1 Reply Last reply 2 Oct 2016, 10:46
    0
    • P Payx
      2 Oct 2016, 10:40

      Thanks but how to return it ?

      ""ui->label_3->setText( QString("Size: %1, %2").arg(s.width()).arg(s.height()) );""

      there is not more simple ?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Oct 2016, 10:46 last edited by
      #19

      @Payx
      Well You need to convert to text anyway so it will become

      ui->label_3->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.width()) );

      Not sure its better.

      • Thanks but how to return it ?
        What we do there is to convert to text and set that text in some label.
        That is not really returning it.

      returning it would be something like

      QSize SomeFunc() {
      return pm.size();
      }

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Payx
        wrote on 2 Oct 2016, 11:09 last edited by
        #20

        Thanks it works. I prefer : ui->label_3->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.height()) );
        because i understand all.

        Then if i want to use height and width for other calcul i can use :
        s.width() * 5 ?

        If i want to cut my picture in many square or rectangle i have to do :

        int h = s.width() and work with h thats right ?

        M 1 Reply Last reply 2 Oct 2016, 15:02
        0
        • P Payx
          2 Oct 2016, 11:09

          Thanks it works. I prefer : ui->label_3->setText( "Size: " + QString::number(s.width()) +" "+ QString::number(s.height()) );
          because i understand all.

          Then if i want to use height and width for other calcul i can use :
          s.width() * 5 ?

          If i want to cut my picture in many square or rectangle i have to do :

          int h = s.width() and work with h thats right ?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 2 Oct 2016, 15:02 last edited by
          #21

          @Payx

          • int h = s.width() and work with h thats right ?
            Yes that's a copy of the current width. ( so w is better name ;)
            but its makes the calculation easier to read than using s.width() directly.

          Note to actually cut the image, you can use
          http://doc.qt.io/qt-5/qimage.html#copy

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Payx
            wrote on 2 Oct 2016, 15:23 last edited by
            #22

            Okay !

            but : QImage QImage::copy(const QRect &rectangle = QRect()) const
            with this code we can work only in one rectangle no ?

            for example i thought cut my picture in pixels 4*4 and have a new image with resolution / 4 :

            i have to use a code like this :

            for (i=0,i<s.width()+1,i=i+4)
            for (j=0,j<s.height()+1,j=j+4)

             end 
            

            end

            M 1 Reply Last reply 2 Oct 2016, 15:54
            0
            • P Payx
              2 Oct 2016, 15:23

              Okay !

              but : QImage QImage::copy(const QRect &rectangle = QRect()) const
              with this code we can work only in one rectangle no ?

              for example i thought cut my picture in pixels 4*4 and have a new image with resolution / 4 :

              i have to use a code like this :

              for (i=0,i<s.width()+1,i=i+4)
              for (j=0,j<s.height()+1,j=j+4)

               end 
              

              end

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 2 Oct 2016, 15:54 last edited by
              #23

              @Payx

              int step=4;
              for (i=0,i<s.width()+1,i=i+step)
                for (j=0,j<s.height()+1,j=j+step)
                just use  QImage QImage::copy(int x, int y, int width, int height) const
              

              This will generate small images of each 4x4

              P 1 Reply Last reply 6 Oct 2016, 13:01
              1
              • P Payx
                1 Oct 2016, 22:21

                I put this "file:///C:/Users/David/Documents/addddd/r.jpg"

                Yes i added a Qlabel with the .ui and named label (dont worry i changed the name in my code)

                P Offline
                P Offline
                Pradeep Kumar
                wrote on 2 Oct 2016, 16:43 last edited by
                #24

                @Payx said in How to display a picture:

                I put this "file:///C:/Users/David/Documents/addddd/r.jpg"

                Yes i added a Qlabel with the .ui and named label (dont worry i changed the name in my code)

                A small info, u can also place image in resource file of the project , so no need to worry about absolute path.

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply
                2
                • M mrjj
                  2 Oct 2016, 15:54

                  @Payx

                  int step=4;
                  for (i=0,i<s.width()+1,i=i+step)
                    for (j=0,j<s.height()+1,j=j+step)
                    just use  QImage QImage::copy(int x, int y, int width, int height) const
                  

                  This will generate small images of each 4x4

                  P Offline
                  P Offline
                  Payx
                  wrote on 6 Oct 2016, 13:01 last edited by
                  #25

                  @mrjj said in How to display a picture:

                  @Payx

                  int step=4;
                  for (i=0,i<s.width()+1,i=i+step)
                    for (j=0,j<s.height()+1,j=j+step)
                    just use  QImage QImage::copy(int x, int y, int width, int height) const
                  

                  This will generate small images of each 4x4

                  Okay i will test that.

                  I got an other question, with that code : "QImage img("C:/path/filename.jpg");" i have to put the destination of the file (sorry for my english)

                  but if i want to create a file explorer (for example in facebook we can change a picture with a file explorer and choose what picture i want)
                  what can i do ?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 6 Oct 2016, 13:42 last edited by mrjj 10 Jun 2016, 13:43
                    #26
                    • but if i want to create a file explorer (for example in facebook we can change a picture with a file explorer and choose what picture i want)

                    Im not 100% sure what you ask, but I give a guess:

                    Instead of using a fixed path in the program you can use
                    http://doc.qt.io/qt-5/qfiledialog.html
                    to let user browser and select an image.

                    Like in this example
                    http://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html

                    so
                    QImage img("C:/path/filename.jpg"
                    becomes

                    QString imagefile = QFileDialog::getOpenFileNames(this, tr("Files"), QDir::currentPath(), tr("*.jpg *.png"));
                    QImage img(imagefile);

                    Hope that is what you asked about.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Payx
                      wrote on 6 Oct 2016, 13:50 last edited by
                      #27

                      I found what i sought :

                      QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Image"),"/path",tr("Image Files (*.jpg)"));

                      But i got all files, and i just want .jpg for now """"""tr("Image Files (*.jpg)"));"""""""""" dont work ?

                      then i want to put my picture file in a label, but i can't find how to convert a QStringList to a Qimage or Qpixmap

                      M 1 Reply Last reply 6 Oct 2016, 14:07
                      0
                      • P Payx
                        6 Oct 2016, 13:50

                        I found what i sought :

                        QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Image"),"/path",tr("Image Files (*.jpg)"));

                        But i got all files, and i just want .jpg for now """"""tr("Image Files (*.jpg)"));"""""""""" dont work ?

                        then i want to put my picture file in a label, but i can't find how to convert a QStringList to a Qimage or Qpixmap

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 6 Oct 2016, 14:07 last edited by mrjj 10 Jun 2016, 14:23
                        #28

                        @Payx
                        it should work with *.jpg.

                        • but i can't find how to convert a QStringList to a Qimage or Qpixmap

                        Oh just use
                        fileName = QFileDialog::getOpenFileName(this,
                        tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));

                        It only returns the 1 filename. ( NOTE THE MISSING s)

                        ui->mylabel->setPixmap( new QPixmap(fileName ));

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Payx
                          wrote on 6 Oct 2016, 14:16 last edited by
                          #29

                          I got one error : Expected one specifier before pixmap

                          M 1 Reply Last reply 6 Oct 2016, 14:22
                          0
                          • P Payx
                            6 Oct 2016, 14:16

                            I got one error : Expected one specifier before pixmap

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 6 Oct 2016, 14:22 last edited by mrjj 10 Jun 2016, 21:00
                            #30

                            @Payx
                            the class is QPixmap
                            so its "new QPixmap"

                            setPixmap( new QPixmap(fileName ));

                            Sorry, was just fast code.

                            1 Reply Last reply
                            1
                            • P Offline
                              P Offline
                              Payx
                              wrote on 6 Oct 2016, 14:38 last edited by
                              #31

                              Yes i tried too before post but i had one error too, so i post the first error.

                              The second is :
                              No matching function for call to 'Qlabel::setPixmap(QPixmap*)"

                              M 1 Reply Last reply 6 Oct 2016, 20:59
                              2
                              • P Payx
                                6 Oct 2016, 14:38

                                Yes i tried too before post but i had one error too, so i post the first error.

                                The second is :
                                No matching function for call to 'Qlabel::setPixmap(QPixmap*)"

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 6 Oct 2016, 20:59 last edited by mrjj 10 Jun 2016, 21:02
                                #32

                                @Payx
                                Sorry not enough coffee :)

                                QPixmap pix(fileName );
                                setPixmap(pix);

                                1 Reply Last reply
                                3
                                • P Offline
                                  P Offline
                                  Pradeep Kumar
                                  wrote on 7 Oct 2016, 08:37 last edited by Pradeep Kumar 10 Jul 2016, 08:38
                                  #33

                                  If u are using QImage

                                  m_imageLabel = new QLabel;
                                  QImage imagePixmap;
                                  imagePixmap.load(":/new/prefix1/Images/imageName.extension");
                                  m_imageLabel->setPixmap(QPixmap::fromImage(imagePixmap));
                                  

                                  Or

                                  Only QPixmap

                                  m_imageLabel = new QLabel;
                                  QPixmap imagePixmap;
                                  imagePixmap.load(":/new/prefix1/Images/imageName.extension");
                                  m_imageLabel->setPixmap(imagePixmap);
                                  

                                  imageName.extension can be image.png, image.jpeg.

                                  Pradeep Kumar
                                  Qt,QML Developer

                                  P 1 Reply Last reply 7 Oct 2016, 08:55
                                  2
                                  • P Pradeep Kumar
                                    7 Oct 2016, 08:37

                                    If u are using QImage

                                    m_imageLabel = new QLabel;
                                    QImage imagePixmap;
                                    imagePixmap.load(":/new/prefix1/Images/imageName.extension");
                                    m_imageLabel->setPixmap(QPixmap::fromImage(imagePixmap));
                                    

                                    Or

                                    Only QPixmap

                                    m_imageLabel = new QLabel;
                                    QPixmap imagePixmap;
                                    imagePixmap.load(":/new/prefix1/Images/imageName.extension");
                                    m_imageLabel->setPixmap(imagePixmap);
                                    

                                    imageName.extension can be image.png, image.jpeg.

                                    P Offline
                                    P Offline
                                    Payx
                                    wrote on 7 Oct 2016, 08:55 last edited by
                                    #34

                                    @Pradeep-Kumar said in How to display a picture:

                                    If u are using QImage

                                    m_imageLabel = new QLabel;
                                    QImage imagePixmap;
                                    imagePixmap.load(":/new/prefix1/Images/imageName.extension");
                                    m_imageLabel->setPixmap(QPixmap::fromImage(imagePixmap));
                                    

                                    Or

                                    Only QPixmap

                                    m_imageLabel = new QLabel;
                                    QPixmap imagePixmap;
                                    imagePixmap.load(":/new/prefix1/Images/imageName.extension");
                                    m_imageLabel->setPixmap(imagePixmap);
                                    

                                    imageName.extension can be image.png, image.jpeg.

                                    U didnt read the thread i think :-).
                                    But thx

                                    @mrjj Lol no problem :-)
                                    Thanks it works i will create an other thread for an other question after

                                    P 1 Reply Last reply 7 Oct 2016, 10:19
                                    2
                                    • P Offline
                                      P Offline
                                      Pradeep Kumar
                                      wrote on 7 Oct 2016, 09:01 last edited by
                                      #35

                                      Thread says how to display a picture

                                      @Payx
                                      Is this the thing?.
                                      correct me if i was wrong.

                                      Pradeep Kumar
                                      Qt,QML Developer

                                      P 1 Reply Last reply 7 Oct 2016, 09:08
                                      1
                                      • P Pradeep Kumar
                                        7 Oct 2016, 09:01

                                        Thread says how to display a picture

                                        @Payx
                                        Is this the thing?.
                                        correct me if i was wrong.

                                        P Offline
                                        P Offline
                                        Payx
                                        wrote on 7 Oct 2016, 09:08 last edited by
                                        #36

                                        @Pradeep-Kumar said in How to display a picture:

                                        Thread says how to display a picture

                                        @Payx
                                        Is this the thing?.
                                        correct me if i was wrong.

                                        wow u just answer the question after 5 guys answer me.

                                        1 Reply Last reply
                                        1
                                        • P Offline
                                          P Offline
                                          Pradeep Kumar
                                          wrote on 7 Oct 2016, 09:48 last edited by
                                          #37

                                          Hmmmmmmmmmm was toooooooooooo late.:)))

                                          Pradeep Kumar
                                          Qt,QML Developer

                                          1 Reply Last reply
                                          1

                                          27/43

                                          6 Oct 2016, 13:50

                                          • Login

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