1. 程式人生 > >C++部分接受圖片位元組陣列,在QML中用Image將圖片載入顯示出來

C++部分接受圖片位元組陣列,在QML中用Image將圖片載入顯示出來

--------------------------------image.h------------------------------------

 Q_PROPERTY(QString strData MEMBER m_strData NOTIFY strDataChanged)

 protected:

 QString m_strData;

 signals:
    void strDataChanged(QString);

------------------------------------image.cpp----------------------------------------

    testTimer5s->start(100);
    connect(testTimer5s, SIGNAL(timeout()), this, SLOT(getEmitData(/*QString*/)));

void CarStatus:: getEmitData(/*QString data*/){
qDebug()<<"write start------";
    num++;
    QFile file(":/classicLeftImages/images/classic_images/leftImage/rpm"+QString::number(num)+".png");
    file.open(QIODevice::ReadOnly);
    int length ;
    QByteArray imageData;
    imageData = file.readAll();
    length = imageData.length();
    //qDebug()<<imageData;

    QFile filew("/home/ymk/testImage/rpm"+QString::number(num)+".png");
            if (filew.open(QIODevice::Append))
                filew.write(imageData);

            file.close();
    qDebug()<<"write finish";
    qDebug()<<"num="<<num;

    m_strData = "file:///home/ymk/testImage/rpm"+QString::number(num)+".png";
    emit strDataChanged(m_strData);
    imageData.clear();
    if(num==7)
        num=0;
}

----------------------------image.qml--------------------------

 property string imageUrl:HtCarStatus.strData

    Image {
        id: imageW
        anchors.centerIn: parent
        source: imageUrl
    }