52

I am new to Qt, and I am trying to create a simple GUI Application that displays an image once a button has been clicked on.

I can read the image in a QImage object, but is there any simple way to call a Qt function that takes the QImage as an input, and displays it?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Karim
  • 1,526
  • 4
  • 13
  • 16

5 Answers5

79

Simple, but complete example showing how to display QImage might look like this:

#include <QtGui/QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QImage myImage;
    myImage.load("test.png");

    QLabel myLabel;
    myLabel.setPixmap(QPixmap::fromImage(myImage));

    myLabel.show();

    return a.exec();
}
Palmik
  • 2,675
  • 16
  • 13
31

Drawing an image using a QLabel seems like a bit of a kludge to me. With newer versions of Qt you can use a QGraphicsView widget. In Qt Creator, drag a Graphics View widget onto your UI and name it something (it is named mainImage in the code below). In mainwindow.h, add something like the following as private variables to your MainWindow class:

QGraphicsScene *scene;
QPixmap image;

Then just edit mainwindow.cpp and make the constructor something like this:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    image.load("myimage.png");
    scene = new QGraphicsScene(this);
    scene->addPixmap(image);
    scene->setSceneRect(image.rect());

    ui->mainImage->setScene(scene);
}
Scott Griffiths
  • 684
  • 6
  • 8
  • 3
    Thanks. I was wondering about a less kludgy way of doing it myself. Maybe I'm weird, but I think a label should be a label, not a picture. – Tyler Jun 20 '14 at 08:47
  • 2
    This does not seem very convenient for displaying lots of images per second though :( – Tomáš Zato Mar 02 '17 at 21:12
  • @TomášZato Calling `setPixmap` on the returned `QGraphicsPixmapItem` to change the image seems pretty convenient. That method can be found by reading the documentation. – Jason C Oct 08 '22 at 02:46
14

One common way is to add the image to a QLabel widget using QLabel::setPixmap(), and then display the QLabel as you would any other widget. Example:

#include <QtGui>

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  QPixmap pm("your-image.jpg");
  QLabel lbl;
  lbl.setPixmap(pm);
  lbl.show();
  return app.exec();
}
Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
  • 1
    Thanks a lot, this is working very fine, I just moved it inside the callback of the button. – Karim Dec 17 '10 at 20:39
8

Thanks All, I found how to do it, which is the same as Dave and Sergey:

I am using QT Creator:

In the main GUI window create using the drag drop GUI and create label (e.g. "myLabel")

In the callback of the button (clicked) do the following using the (*ui) pointer to the user interface window:

void MainWindow::on_pushButton_clicked()
{
     QImage imageObject;
     imageObject.load(imagePath);
     ui->myLabel->setPixmap(QPixmap::fromImage(imageObject));

     //OR use the other way by setting the Pixmap directly

     QPixmap pixmapObject(imagePath");
     ui->myLabel2->setPixmap(pixmapObject);
}
Karim
  • 1,526
  • 4
  • 13
  • 16
4

As far as I know, QPixmap is used for displaying images and QImage for reading them. There are QPixmap::convertFromImage() and QPixmap::fromImage() functions to convert from QImage.

rachana
  • 3,344
  • 7
  • 30
  • 49
Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
  • From the [official documentation](https://doc.qt.io/qt-6/qpixmap.html): `Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. The isQBitmap() function returns true if a QPixmap object is really a bitmap, otherwise returns false. Finally, the QPicture class is a paint device that records and replays QPainter commands.` – danieltakeshi May 25 '23 at 18:12