P. Zuñiga
P. Zuñiga

Reputation: 11

QT C++ : Moving several labels at the same time

I have been searching about Threads in QT and ways to process multiple events simultaneously according to their docs, only the main GUI thread can manage GUI related events. So my question is: Is it possible to move multiple labels or objects at the same time during execution? Im trying to create sort of a simulation proyect for school.

What I have right now is: it creates a label everytime I run the function so I need that label to move to a certain spot. Problem is since I need the function executed several times, when it executes again, the previous label stops and moves the new one. After it has completed it goes back to the previous.

Any help is appreciated thanks.

New to asking here and QT in general.

Edit:

Here is what I have of my function:

QLabel *cliente = new QLabel(this);
    QPixmap pix("image.jpg");
    cliente->setGeometry(10,50,128,128);
    cliente->setPixmap(pix);
    cliente->show();
    int speed = 100;
    while (cliente->x()<300){
        QTime dieTime = QTime::currentTime().addMSecs(speed);
        while (QTime::currentTime() < dieTime){
            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
        }
        cliente->move(cliente->x()+10,cliente->y());

    }

Upvotes: 1

Views: 1094

Answers (1)

eyllanesc
eyllanesc

Reputation: 244132

To handle the movement of widgets it is most advisable to use the QPropertyAnimation class, but if you want to handle parallel group animations it is opportune to use QParallelAnimationGroup as shown in the following example:

#include <QApplication>
#include <QLabel>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    w.resize(500, 550);

    QParallelAnimationGroup group;

    for(int i = 0; i < 10; i++){
        QLabel *label = new QLabel(QString("label %1").arg(i), &w);
        QPropertyAnimation *animation = new QPropertyAnimation(label, "pos");
        animation->setDuration(1000);
        animation->setStartValue(QPoint(50*i, 0));
        animation->setEndValue(QPoint(50*i, 50*(i+1)));
        group.addAnimation(animation);
    }
    group.start();
    w.show();

    return a.exec();
}

Output:

enter image description here

Upvotes: 3

Related Questions