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
QtWS25 Last Chance

How to display a picture

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 8 Posters 85.0k 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 1 Oct 2016, 19:49 last edited by koahnig 10 Jan 2016, 19:54
    #1

    Hello guys,

    I'm a beginner in Qt and i want to display a picture in Qt and i want to do whatever i want with ( with a label i think) : Like calculate resolution, reduce it....

    I try a lot of code fought in internet but no one works on me.

    I try this alone but it display nothing :

    voidmain()
    {
    QImage myImage;
    myImage.load("Revolt.jpg");
    }
    

    [edit koahnig: introduction of code wrappers]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 1 Oct 2016, 19:53 last edited by
      #2

      Hi and welcome to devnet

      Did you see this image viewer example?

      Probably the best is to study that example and see what you have to change for purposes.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Payx
        wrote on 1 Oct 2016, 20:40 last edited by VRonin 10 Mar 2016, 07:39
        #3

        Thanks for your time.

        I read the tutorial, i stopped at this code :

        The function loadFile() is used to load the image.

        bool ImageViewer::loadFile(const QString &fileName)
        {
            QImageReader reader(fileName);
            reader.setAutoTransform(true);
            const QImage newImage = reader.read();
            if (newImage.isNull()) {
                QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
                                         tr("Cannot load %1: %2")
                                         .arg(QDir::toNativeSeparators(fileName), reader.errorString()));
                return false;
            }
        

        I tried this an i got a lot of error...

        Can i just have a little code for just display a picture with a label if you can please ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Oct 2016, 20:50 last edited by
          #4

          Hi,

          You should read the full source of the example here.

          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
          • P Offline
            P Offline
            Payx
            wrote on 1 Oct 2016, 20:55 last edited by
            #5

            Thank you, but i was saying is that i got a lot of error.

            So i need an image reader to load the picture if i get it.
            Than can i use a command like this : ui->label->(probably set picture to a label ?)

            ? 1 Reply Last reply 1 Oct 2016, 21:17
            0
            • P Payx
              1 Oct 2016, 20:55

              Thank you, but i was saying is that i got a lot of error.

              So i need an image reader to load the picture if i get it.
              Than can i use a command like this : ui->label->(probably set picture to a label ?)

              ? Offline
              ? Offline
              A Former User
              wrote on 1 Oct 2016, 21:17 last edited by A Former User 10 Jan 2016, 21:18
              #6

              @Payx Hi! Showing an image with a QLabel is super easy:

              In your mainwindow.cpp just add the following:

              // ...
              #include <QPixmap>
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  QPixmap pm("C:/Users/Patrick Wieland/Documents/Kochen/bento.jpg"); // <- path to image file
                  ui->label_2->setPixmap(pm);
                  ui->label_2->setScaledContents(true);
              }
              // ...
              
              1 Reply Last reply
              8
              • P Offline
                P Offline
                Payx
                wrote on 1 Oct 2016, 21:31 last edited by
                #7

                I got this error with your code :

                C:\Users\David\Documents\projet\mainwindow.cpp:17: erreur : redefinition of 'MainWindow::MainWindow(QWidget*)'
                MainWindow::MainWindow(QWidget *parent) :
                ^

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 1 Oct 2016, 21:33 last edited by
                  #8

                  You need to add #include <QPixmap> to the other includes and add these three lines...

                  QPixmap pm("C:/Users/Patrick Wieland/Documents/Kochen/bento.jpg"); // <- path to image file
                  ui->label_2->setPixmap(pm);
                  ui->label_2->setScaledContents(true);
                  

                  to your existing MainWindow constructor.

                  1 Reply Last reply
                  3
                  • P Offline
                    P Offline
                    Payx
                    wrote on 1 Oct 2016, 21:39 last edited by
                    #9

                    Okay sorry.

                    I did it but nothing appear in my MainWindow

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on 1 Oct 2016, 21:42 last edited by A Former User 10 Jan 2016, 23:03
                      #10

                      Did you put in the correct path to your image file? The path in the code above is only valid on my harddrive ;-) Also, did you add a QLabel to your MainWindow? I added a QLabel with the designer in Qt Creator and the name of that label is label_2.

                      1 Reply Last reply
                      1
                      • P Offline
                        P Offline
                        Payx
                        wrote on 1 Oct 2016, 22:21 last edited by
                        #11

                        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 1 Reply Last reply 2 Oct 2016, 16:43
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on 1 Oct 2016, 22:24 last edited by
                          #12

                          Only type "C:/Users/David/Documents/addddd/r.jpg", without the "file://". The pixmap constructor takes a file path and not an URL.

                          1 Reply Last reply
                          3
                          • P Offline
                            P Offline
                            Payx
                            wrote on 1 Oct 2016, 22:33 last edited by
                            #13

                            Its okay it works ! Thanks a lot for your time

                            To works now with the Qlabel and for example i would like to calculate the numbers of pixels (number of x pixels and y pixels) do you have any tutorial for this ?

                            ? 1 Reply Last reply 1 Oct 2016, 22:44
                            1
                            • P Payx
                              1 Oct 2016, 22:33

                              Its okay it works ! Thanks a lot for your time

                              To works now with the Qlabel and for example i would like to calculate the numbers of pixels (number of x pixels and y pixels) do you have any tutorial for this ?

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on 1 Oct 2016, 22:44 last edited by A Former User 10 Feb 2016, 11:05
                              #14

                              @Payx Add another QLabel (here its name is label_3) to the MainWindow. We can use that label to display the size of the Pixmap. When adding the label in Qt Creator's Designer, make the label wide enough so that all the text fits in.

                              In the MainWindow constructor in mainwindow.cpp:

                              const QSize s = pm.size(); // <- get size of the Pixmap
                              ui->label_3->setText( QString("Size: %1, %2").arg(s.width()).arg(s.height()) );
                              

                              Edit: Pixmap has a lot of built-in stuff, see QPixmap Class for more. QPixmap is there to display images, to actually put them on the screen. If you want to manipulate an image, you don't use a pixmap for that. Use a QImage then, see: QImage Class.

                              So, this is the workflow:

                              1. Create a QImage from a file: QImage img("C:/path/filename.jpg");
                              2. Do something with that image, like change the color of some pixels.
                              3. Create a QPixmap from that QImage: QPixmap pm = QPixmap::fromImage(img);
                              4. Show the Pixmap in a QLabel: ui->label->setPixmap(pm);
                              1 Reply Last reply
                              3
                              • P Offline
                                P Offline
                                Payx
                                wrote on 2 Oct 2016, 09:49 last edited by
                                #15

                                So if i understand, i use QImage for doing whatever i want with this image.
                                And i use Qpixmax just for display in a label ?

                                (sorry for my english im not bilingual)

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  Payx
                                  wrote on 2 Oct 2016, 10:00 last edited by
                                  #16

                                  So if i want to calculate the size of my picture i use :

                                  QSize QImage::size() const

                                  The question is : how to use it ? there is no example

                                  M 1 Reply Last reply 2 Oct 2016, 10:21
                                  0
                                  • P Payx
                                    2 Oct 2016, 10:00

                                    So if i want to calculate the size of my picture i use :

                                    QSize QImage::size() const

                                    The question is : how to use it ? there is no example

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 2 Oct 2016, 10:21 last edited by mrjj 10 Feb 2016, 10:23
                                    #17

                                    @Payx said in How to display a picture:

                                    QImage::size()

                                    It returns a
                                    http://doc.qt.io/qt-5/qsize.html
                                    and you can ask it height / width etc
                                    http://doc.qt.io/qt-5/qsize.html#height

                                    const QSize MySize = theImage->size();

                                    int h=MySize.height();

                                    etc.
                                    Update:
                                    @wieland already showed ;)

                                    1 Reply Last reply
                                    1
                                    • 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

                                          10/43

                                          1 Oct 2016, 21:42

                                          33 unread
                                          • Login

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