views:

4841

answers:

4

I want to display .jpg image in an Qt UI. I checked it online and found http://doc.trolltech.com/4.2/widgets-imageviewer.html. I thought Graphics View will do the same, and also it has codec to display video. How to display images using Graphics View? I went through the libraries, but because I am a totally newbie in Qt, I can't find a clue to start with. Can you direct me to some resources/examples on how to load and display images in Qt?

Thanks.

+3  A: 
#include ...

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QGraphicsPixmapItem item(QPixmap("c:\\test.png"));
    scene.addItem(&item);
    view.show();
    return a.exec();
}

This should work. :) List of supproted formats can be found here

wuub
how can we use this with UI created in QT Creator?
DucDigital
Please note that for Qt 4.6 this code has an error. Try this one:int main(int argc, char *argv[]){ QString fileName("C:/aaa..gif"); QApplication a(argc, argv); QGraphicsScene scene; scene.addPixmap(QPixmap(fileName)); QGraphicsView view( view.show(); return a.exec();}
Narek
+2  A: 

If the only thing you want to do is drop in an image onto a widget withouth the complexity of the graphics API, you can also just create a new QWidget and set the background with StyleSheets. Something like this:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    ...
    QWidget *pic = new QWidget(this);
    pic->setStyleSheet("background-image: url(test.png)");
    pic->setGeometry(QRect(50,50,128,128));
    ...
}
+3  A: 

You could attach the image (as a pixmap) to a label then add that to your layout...

...

QPixmap image = new QPixmap("blah.jpg");

QLabel imageLabel = new QLabel();
imageLabel.setPixmap(image);

mainLayout.addWidget(imageLabel);

...

Apologies, this is using Jambi (Qt for Java) so the syntax is different, but the theory is the same.

Ben L
A: 

I want to display .jpg image in an Qt UI

The simpliest way is to use QLabel for this:

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    QLabel label("<img src='image.jpg' />");
    label.show();
    return a.exec();
}
Vanuan