Reputation: 113
I am trying to make a basic window that shows a .gif but it doesn't. I made sure that the file is in the same directory than my main.cpp . I tried with other file extensions but it still doesn't work.
#include <QApplication>
#include <QBoxLayout>
#include <QLabel>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
window->setFixedSize(640, 480);
QLabel *gif_anim = new QLabel();
QPixmap const *movie = new QPixmap("sea.gif");
gif_anim->setPixmap(*movie);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(gif_anim);
std::cout << movie->isNull() << std::endl;
window->setLayout(layout);
window->show();
return a.exec();
}
I tested with the isNull() method and it tells me that my QPixmap is null (I don't know why).
Your help is appreciated.
Upvotes: 1
Views: 160
Reputation: 1611
I use QMovie for gif animations instead of QPixmap:
QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie("sea.gif");
gif_anim->setMovie(*movie);
movie->start();
I usually check the supported image formats in Qt. You can do it with this code:
qDebug() << "Supported image formats: " << QImageReader::supportedImageFormats();
Upvotes: 2
Reputation: 243955
You have the following errors:
QPixmap does not support the gif format, if you want to show a gif you must use QMovie.
The path of the file is relative to the executable, not the source code, so in these I use a small command to copy files from the source code folder to the build folder.
Considering the above, the solution is:
*.pro
COPY_CONFIG = sea.gif
copy_cmd.input = COPY_CONFIG
copy_cmd.output = ${QMAKE_FILE_IN_BASE}${QMAKE_FILE_EXT}
copy_cmd.commands = $$QMAKE_COPY_DIR ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
copy_cmd.CONFIG += no_link_no_clean
copy_cmd.variable_out = PRE_TARGETDEPS
QMAKE_EXTRA_COMPILERS += copy_cmd
main.cpp
QWidget window;
window.setFixedSize(640, 480);
QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie("sea.gif");
gif_anim->setMovie(movie);
movie->start();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(gif_anim);
window.setLayout(layout);
window.show();
Upvotes: 1