ZBLOG

c++ json动画绘制到QLabel中代码示例

下面是一个示例代码,将C++中的JSON动画绘制到Qt中的QLabel控件中:

#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QPainter>
#include <QTimer>
#include <QWidget>

class AnimationWidget : public QWidget {
public:
    AnimationWidget(QWidget* parent = nullptr)
        : QWidget(parent), frameIndex(0) {
        setFixedSize(500, 500);

        // 定义JSON动画帧数据
        jsonAnimation = R"(
            {
                "frames": [
                    {"x": 0, "y": 0, "width": 100, "height": 100},
                    {"x": 100, "y": 0, "width": 100, "height": 100},
                    {"x": 200, "y": 0, "width": 100, "height": 100}
                ],
                "frameRate" : 10
            }
        )";

        // 解析JSON数据
        QJsonDocument doc = QJsonDocument::fromJson(jsonAnimation.toUtf8());
        QJsonObject animationObject = doc.object();
        frameRate = animationObject["frameRate"].toInt();
        framesArray = animationObject["frames"].toArray();

        // 创建定时器来更新动画帧
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this,
                SLOT(updateFrame()));
    }

protected:
    void paintEvent(QPaintEvent* event) override {
        QPainter painter(this);

        if (currentFrame.isNull())
            return;

        painter.fillRect(rect(), Qt::white);
        
        // 在QLabel中绘制当前帧图像
        painter.drawImage(currentFrame.x(), currentFrame.y(),
                          QImage(":/path_to_image.png"));

        QWidget::paintEvent(event);
    }

private slots:
    void updateFrame() {
        // 更新当前帧
        if (frameIndex >= framesArray.size())
            frameIndex = 0;

        QJsonObject frameObject =
            framesArray.at(frameIndex).toObject();
        int x = frameObject["x"].toInt();
        int y = frameObject["y"].toInt();
        int width = frameObject["width"].toInt();
        int height = frameObject["height"].toInt();

        currentFrame.setRect(x, y, width, height);
        
        // 重绘控件
        update();

        ++frameIndex;
    }

public:
    void startAnimation() {
        timer->start(1000 / frameRate); // 设置定时器的间隔时间
    }

private:
    QTimer* timer;
    QString jsonAnimation;
    QJsonArray framesArray;
    QRectF currentFrame;
    int frameIndex;
    int frameRate;
};

int main(int argc, char** argv) {
    QApplication app(argc, argv);

    AnimationWidget widget;
    
	QLabel label(&widget);
	label.move(10, 10);
	label.resize(500, 500);
	
	widget.show();
	widget.startAnimation();

	return app.exec();
}

请注意,上述代码中的路径 :/path_to_image.png 需要替换为您实际使用的图像文件路径。此外,还需确保在项目文件(.pro)中添加 QT += widgets

这个示例使用QTimer定时器来更新动画帧,并通过在paintEvent函数中绘制当前帧图像将其显示在QLabel控件上。您可以根据自己的需要进行修改和调整。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=8947

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?