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

标签 c++ qt qt5

我一直在根据他们的文档搜索 QT 中的线程以及同时处理多个事件的方法,只有主 GUI 线程可以管理 GUI 相关事件。所以我的问题是:是否可以在执行过程中同时移动多个标签或对象?我正在尝试为学校创建一种模拟项目。

我现在拥有的是:它会在我每次运行该函数时创建一个标签,因此我需要将该标签移动到某个位置。问题是因为我需要多次执行函数,当它再次执行时,前一个标签停止并移动新标签。完成后,它会返回到上一个。

感谢任何帮助。

在这里提问和一般的 QT 是新手。

编辑:

这是我的函数:

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());

    }

最佳答案

要处理小部件的移动,最明智的做法是使用 QPropertyAnimation类,但如果你想处理并行组动画,使用 QParallelAnimationGroup 是合适的如下例所示:

#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();
}

输出:

enter image description here

关于c++ - QT C++ : Moving several labels at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529155/

相关文章:

qt - 在 Qt 中,设置环境变量的不同方法有哪些,优缺点是什么?

c++ - mkdir Windows 与 Linux

C++ 使用暂停/恢复控件下载文件

c++ - 当排序标准需要额外的变量时,如何对列表进行排序? C++

c++ - C 和 C++ 中的原始内存是什么?

c++ - ListView 最初不显示数据

c++ - 如何将 QPlainText 中的文本方向更改为从右到左?

qt - 为什么我的 Qt5 语言翻译未加载?

c++ - QWidget失去了它的 parent

QtQuick : cannot playback any video on embedded linux