QPropertyAnimation loop continuously
-
My code below fails to make animation loop continuously. Please kindly assist.
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(); label= new QLabel; movie = new QMovie("/home/ubuntu/Desktop/graphic.gif"); label->setMovie(movie); movie->start(); int i; do{ animation = new QPropertyAnimation(label,"geometry"); animation->setDuration(1000); animation->setStartValue(QRect(0,200,100,30)); animation->setEndValue(QRect(600,200,100,30)); animation->start(QAbstractAnimation::DeleteWhenStopped); i++; }while(i<100); QTimer *timer = new QTimer(); //connect(timer,SIGNAL(timeout()),movie,SLOT(move())); 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();
-
Did you try setLoopCount() ?
-
For looping do what @p3c0 said.
I've got to ask though - do you really intend to start 100 animations of the same label and geometry at once? This seems weird and incorrect at best.
In any caseint i;
has undefined value in c++ (no, it's not initialized to 0 automatically).
And what's the timer for here? It doesn't seem to do anything. -
Thanks for all the help given. i have 3 more Question
Question 1
animation->start(QAbstractAnimation::DeleteWhenStopped)
The animation doesn't seem to delete itself when stopped?Question 2
Strange, i can't make 2 or multiple label appear at the same time.
How do i achieve this?QTimer *timer = new QTimer(); connect(timer,SIGNAL(timeout()),this,SLOT(move())); timer->start(500); void MainWindow::move() { animation = new QPropertyAnimation(label,"geometry"); animation->setDuration(1000); animation->setLoopCount(4); animation->setStartValue(QRect(0,990,30,30)); animation->setEndValue(QRect(4000,990,30,30)); animation->start(QAbstractAnimation::DeleteWhenStopped);
}
Question 3
qt allocates a big horizontal tab for label. Very Ugly.
How to ask qt in code to allocate a smaller horizontal tab for label?layout->addWidget(videoWidget,0,0); layout->addWidget(videoWidget1,0,1); layout->addWidget(ml,1,0,1,2); layout->addWidget(label,2,0,1,2); //qt allocates a big space for this label window->showMaximized(); window->setLayout(layout); window->show();
-
-
How did you checked that? The code from 2. doesn't track the lifetime of the animation in any way. I suspect it is deleted.
-
This does not create multiple labels. This creates new animations for the same label. The animations are created every 0.5s and last 1s each so after a couple of seconds you've got a dozen of animations fighting to animate the same label.
-
It's not Qt. It's you :) The last parameter in
layout->addWidget(label,2,0,1,2);
is column span. Since you gave it 2 and there are two columns of widgets in that layout it will take the whole width of that layout. Give it a 1 and see if that's what you need.
Bonus 1. As previously mentioned - you won't be able to animate the "geometry" property of your label if it's in a layout. The layout takes it over and setting geometry has no effect.
Bonus 2. The timer has no parent so it's a memory leak.
Bonus 3. What's with the multiple shows? This should be enough:
window -> setLayout(layout); window -> showMaximized();
-
-
"This does not create multiple labels. This creates new animations for the same label. The animations are created every 0.5s and last 1s each so after a couple of seconds you've got a dozen of animations fighting to animate the same label.
connect(timer,SIGNAL(timeout),this,SLOT(move()));
in Theory, if timer has short timeout, move() should executed label closed to each other resulting multiple label appearing.
i can't achieve this, how to amend the code to achieve this?"Bonus 2. The timer has no parent so it's a memory leak"
always parent a constructor [QTimer *timer = new QTimer(this)] else delete object-timer in destructor(which i did)