Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display QImage with QtGui

Tags:

c++

qt

qimage

qtgui

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?

like image 960
Karim Avatar asked Dec 17 '10 19:12

Karim


People also ask

How do you convert QImage to QPixmap?

A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead.

How do I display an image in Qt widget?

There isn't a widget specifically made for displaying images, but this can be done with the label widget. We do this with the pixmap property. QPixmap pic("/path/to/your/image"); ui->label->setPixmap(pic);

What is QImage C++?

The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.


Video Answer


1 Answers

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(); } 
like image 192
Palmik Avatar answered Sep 28 '22 23:09

Palmik