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?
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.
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);
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With