Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to display a picture



  • 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 ?)



  • @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);
    }
    // ...
    


  • 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) :
    ^



  • 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.



  • Okay sorry.

    I did it but nothing appear in my MainWindow



  • 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.



  • 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)



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



  • 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 ?



  • @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);


  • 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)



  • 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


  • Lifetime Qt Champion

    @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 ;)



  • 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 ?


  • Lifetime Qt Champion

    @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();
    }



  • 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 ?


  • Lifetime Qt Champion

    @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



  • 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


  • Lifetime Qt Champion

    @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



  • @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.



  • @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 ?


  • Lifetime Qt Champion

    • 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.



  • 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


  • Lifetime Qt Champion

    @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 ));



  • I got one error : Expected one specifier before pixmap


  • Lifetime Qt Champion

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

    setPixmap( new QPixmap(fileName ));

    Sorry, was just fast code.



  • 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*)"


  • Lifetime Qt Champion

    @Payx
    Sorry not enough coffee :)

    QPixmap pix(fileName );
    setPixmap(pix);



  • 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 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



  • Thread says how to display a picture

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



  • @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.



  • Hmmmmmmmmmm was toooooooooooo late.:)))



  • Thanks it works i will create an other thread for an other question after

    If it is working can u mark thread has solved, so it will be useful in future.
    Cheers.:)))))))

    Thanks,



  • How can i do that ?



  • u have already marked as solved for this thread.



  • Anyways in the right bottom of the topic u will see topic button and u see mark as solved under the topic button, click on it.

    :)

    Thanks.



  • The link address has expired
    Please pay attention to select the example corresponding to your current qt version in the upper right corner
    https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/widgets/imageviewer?h=5.15


  • Banned

    This post is deleted!

Log in to reply