SoFunction
Updated on 2025-03-10

Detailed explanation of several ways to use QtGui to display QImage

Problem description

I'm a newbie who just learned Qt and am trying to create a simple GUI application. When a button is clicked, an image is displayed. I can read the picture using a QImage object, but is there an easy way to call a Qt function that takes QImage as input and displays it?

Method 1: Use QLabel to display QImage

The easiest way is to add QImage to QLabel and pass QLabel'ssetPixmap()Methods to display it. Here is a complete example code:

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

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

    QImage myImage;
    ("");

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

    ();

    return ();
}

This method is simple and straightforward, and is very suitable for beginners.

Method 2: Use QGraphicsView to display QImage

Using QLabel to display images is an easy way, but in newer versions of Qt, you can use the QGraphicsView control, which is more professional and flexible. In Qt Creator, you can drag and drop a Graphics View control into the UI and name it mainImage. Then add the following private variables:

QGraphicsScene *scene;
QPixmap image;

Edit, the constructor can be written like this:

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

    ("");
    scene = new QGraphicsScene(this);
    scene->addPixmap(image);
    scene->setSceneRect(());

    ui->mainImage->setScene(scene);
}

This method uses QGraphicsScene to manage images, allowing for more graphics processing operations to be conveniently performed.

Method 3: Load and display the picture in the button click callback

Use Qt Creator to create the main GUI window, and create a label in the window by dragging and dropping (e.g.myLabel). Do the following in the button click callback function to display the image:

void MainWindow::on_pushButton_clicked()
{
    QImage imageObject;
    (imagePath);
    ui-&gt;myLabel-&gt;setPixmap(QPixmap::fromImage(imageObject));

    // Or use QPixmap object directly    QPixmap pixmapObject(imagePath);
    ui-&gt;myLabel2-&gt;setPixmap(pixmapObject);
}

Method 4: Conversion between QImage and QPixmap

QPixmap is usually used to display images, while QImage is used to read and write images. AvailableQPixmap::convertFromImage()andQPixmap::fromImage()The function converts between the two.

QPixmap pixmap = QPixmap::fromImage(imageObject);

Summarize

Choose the right way to display the image according to different needs and scenarios. If you simply display a picture, using QLabel is the fastest way; if you need more advanced graphics processing, QGraphicsView is more suitable. I hope these methods can help you become more handy in the learning and use of Qt.

This is the end of this article about several methods of QT displaying QImage using QtGui. For more related QT displaying QImage using QtGui, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!