Hi all I want to put loader.gif file into my QMainWindow. I want to have black background and this QMovie item in the middle.
Here is my code and it's not doing anything.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
QMovie *movie = new QMovie(":/slike/Loading_Animations.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start()
//how to assign this to my UI
}
At some point you will need to call processLabel->show()
for your main window to display the movie. If the gif is looped it will repeat infinitely. Otherwise it will just play once.
Since you are inheriting Ui::MainWindow I assume you are using Qt Designer to lay out your window. It may be to your advantage to add processLabel to the user interface within Qt Designer. You can setup the style sheet (add a black background) graphically and then just call ui->setupUi(this)
before adding your movie. Here is some sample code that works for me:
#include <QMovie>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
//calling setupUi creates processLabel and positions it and shows it correctly
QMovie *movie = new QMovie(":/slike/Loading_Animations.gif");
processLabel->setMovie(movie);
movie->start();
//if you haven't already called show for your main window call it
this->show();
}
Otherwise, before you show your QLabel
, make sure you:
QLabel::setGeometry(x,y,width,height)
)QLabel::setScaledContents(true)
QLabel::show()
.