QML中Image动态显示图片内容

1.定义一个ColorImageProvider类

#ifndef COLORIMAGEPROVIDER_H
#define COLORIMAGEPROVIDER_H

#include <QObject>
#include <QImage>
#include <QQuickImageProvider>


#include <QTimer>


class ColorImageProvider :public QObject, public QQuickImageProvider
{
    Q_OBJECT

public:
    ColorImageProvider(QObject* parent = nullptr)
        : QQuickImageProvider(QQuickImageProvider::Image)
    {

        connect(&_timer, &QTimer::timeout, this, [this](){

            int width = 100;
            int height = 50;
            static int n = 0;
            n++;

            QPixmap pixmap( width,
                            height);

            if( n % 2 == 0)
                pixmap.fill(QColor("blue").rgba());
            else
                pixmap.fill(QColor("green").rgba());

            setImage(pixmap.toImage());

        });

    }

    QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override
    {
        if(size) *size = _image.size();

        if(requestedSize.width() > 0 && requestedSize.height() > 0 ){
            return _image.scaled(requestedSize.width(), requestedSize.height(), Qt::KeepAspectRatio);
        }

        return _image;
    }

    void start(int ms){

        _timer.start(ms);
    }

signals:
    void imageUpdated();

private:

    QTimer _timer;
    QImage _image;

    void setImage(const QImage& image){

        if(!image.isNull()){
            _image = image;

            emit imageUpdated();

        }
    }

};

#endif // COLORIMAGEPROVIDER_H

2.main.cpp 注册并定时显示

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include "ColorImageProvider.h"
#include <QQmlContext>


int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);


    QQmlApplicationEngine engine;
    ColorImageProvider* provider = new ColorImageProvider();

    engine.rootContext()->setContextProperty("imageProvider", provider);
    engine.addImageProvider(QLatin1String("stream"), provider);
    provider->start(1000);


    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    engine.load(url);



    return app.exec();
}

3.在main.qml中添加控件

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Connections {
        target: imageProvider
        function onImageUpdated() {
            image1.reload()

        }
    }


    Column {

        Image {
            id: image1

            readonly property string providerSource: "image://stream/";


            fillMode: Image.PreserveAspectFit
            asynchronous: false
            cache: false


            function reload() {
                var oldSource = providerSource;

                console.log(oldSource)
                source = "";
                source = oldSource;

            }

        }

    }


}

4.执行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值