PDA

View Full Version : Cannot open gif in Graphics View



alok9871
27th December 2012, 07:44
I am designing a page which has Graphics View widget. I intend to run a gif in it. I have looked into various documentation and tried running the following code, but i am not getting the result, Please help.

1.Mainwindow.cpp :


ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie("Sin_drawing_process.gif");
gif_anim->setMovie(movie);
movie->start();
proxy = scene->addWidget(gif_anim);


}



2.Mainwindow.h


class MainWindow : public QMainWindow
{
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsProxyWidget *proxy;
}

ChrisW67
27th December 2012, 21:49
What result are you not getting? No proxy widget, no image, no animation?

This code showing an endless gif animation, for example, runs fine here:


#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QGraphicsView view;
QGraphicsScene *scene = new QGraphicsScene(&view);
view.setScene(scene);
QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie("dna_e0.gif");
gif_anim->setMovie(movie);
movie->start();
QGraphicsProxyWidget *proxy = scene->addWidget(gif_anim);
view.show();

return app.exec();
}

alok9871
28th December 2012, 08:57
Hi ChrisW67

Thanks for your reply. Gives me a hint, I am near-about.

Unfortunately, I could not even import an image on to the widget.

Well I tried to draw something inside the graphicsView, I could do it. But importing an image or a gif is something that i haven't been able to achieve. To my code, I changed the background color of the Graphics View, What I found was that at the centre there is a small white rectangle.

Any suggestions on where I have gone wrong?

Added after 35 minutes:

I ran your code, and I get this as output.

8536

ChrisW67
28th December 2012, 19:35
You have one of these two problems:

The image is not in the location you are trying to load it from.
You do not have a GIF image format plugin or you have deployed the program to another computer and not deployed the plugin.


My money is on the first.

The code I gave you depends on having the image file (dna_e0.gif) in the current working directory of the running program. Please note that this is not generally the same as the directory containing the executable or the source code directory. If you are running the program inside Qt Creator with shadow building turned on (the default) the the run-time current working directory is definitely not the source directory, which is most likely where you put the gif file.

You can use QCoreApplication::applicationDirPath () to locate an image relative to the executable directory.
You can use the Qt resource system to embed the image in your executable.

alok9871
31st December 2012, 07:35
Thank you ChrisW67. You are not called Guru for no reason!

I created a Qt Resource file(.qrc) . I put my gif file in the folder--> Right-clicked on the file to copy the resource file path-> pasted it on to the QMovie field--> got the result.

Many thanks