How to setPos in QMovie?
-
How to move QMovie- gif file from bottom left to bottom right?
Below is my code
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include "ticker.h"
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QMovie>
#include <QTimer>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget *window= new QWidget;
window->setWindowTitle("My App");ml = new WidgetMarqueeLabel(this); ml->setTextFormat(Qt::RichText); ml->setAlignment(Qt::AlignBottom); ml->setText("this is a very long text, weather is very hot"); ml->setFont(QFont("Arial", 20,20)); QGridLayout *layout= new QGridLayout; video(); QLabel* label= new QLabel; QMovie* movie = new QMovie("/home/ubuntu/Desktop/graphic.gif"); label->setMovie(movie); movie->start(); QTimer *timer = new QTimer(); connect(timer,SIGNAL(timeout()),movie,SLOT(move())); //??How to move QMovie timer->start(60); layout->addWidget(videoWidget,0,0); layout->addWidget(videoWidget1,0,1); layout->addWidget(ml,1,0,1,2); layout->addWidget(label,2,0,1,2); window->setLayout(layout); window->show();
}
void MainWindow::video()
{
playlist= new QMediaPlaylist();
player= new QMediaPlayer;
videoWidget = new QVideoWidget;
playlist->addMedia(QUrl::fromLocalFile("/opt/VP.mp4"));
player->setPlaylist(playlist);playlist1= new QMediaPlaylist(); player1= new QMediaPlayer; videoWidget1 = new QVideoWidget; playlist1->addMedia(QUrl::fromLocalFile("/opt/hotgirl.mp4")); player1->setPlaylist(playlist1); player->setVideoOutput(videoWidget); player1->setVideoOutput(videoWidget1); playlist->setCurrentIndex(1); playlist1->setCurrentIndex(1); player->play(); player1->play();
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::move()
{
setPos(x()-10,y());}
@ -
Hi and welcome,
There are a couple of problems with your code.First of all you're leaking memory like crazy. window does not have a parent and you never delete it, so it's a leak. The same goes for movie, timer, playlist1, player1 and videoWidget1.
Then, you make a ml a child of MainWindow (by passing this in the constructor), but put it in a layout of window, which switches its parent to window. While not an error, it's just wasteful. Just don't give it a parent if it's going in a layout.Now to the moving problem. You didn't write what setPos is, but I assume your intention is to animate the position of label displaying the movie.
In your code you placed the label inside a layout, so it's the layout that governs the size and position of the label. You can't override it.
If you want to manually set the position of the label don't put it in a layout. Give it a parent (e.g. window) and then use setGeometry.Also, there's a cleaner way to achieve the same without a timer. Check the example of QPropertyAnimation.
-
Thanks Chris. U are an angel.