久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

QML 和 C++ 圖像互操作性

QML and C++ image interoperability(QML 和 C++ 圖像互操作性)
本文介紹了QML 和 C++ 圖像互操作性的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我已經(jīng)瀏覽了文檔以及我可以在互聯(lián)網(wǎng)上找到的所有內(nèi)容,但似乎無法從 C++ 訪問 QML 圖像.

I've looked through the documentation and also whatever I could find on the internet, but it doesn't seem like it is possible to access a QML Image from C++.

有沒有辦法解決這個(gè)問題?

Is there a way to work around that?

推薦答案

在 QtQuick1 中可以實(shí)現(xiàn),但在 QtQuick2 中刪除了該功能.

It was possible to do in QtQuick1 but that functionality was removed in QtQuick2.

我提出的解決方案允許在 QML 和 C++ 中使用相同的圖像,方法是實(shí)現(xiàn)一個(gè) QQuickImageProvider,它基本上與 QPixmap * 一起使用,它被轉(zhuǎn)換為字符串然后回到指針類型(聽起來確實(shí)有點(diǎn)不安全,但事實(shí)證明它工作得很好).

The solution I've come up with allows to have the same image in QML and C++ by implementing a QQuickImageProvider that basically works with QPixmap * which is converted to string and then back to a pointer type(it does sound a little unsafe but has proven to work quite well).

class Pixmap : public QObject {
    Q_OBJECT
    Q_PROPERTY(QString data READ data NOTIFY dataChanged)
public:
    Pixmap(QObject * p = 0) : QObject(p), pix(0) {}
    ~Pixmap() { if (pix) delete pix; }

    QString data() {
        if (pix) return "image://pixmap/" + QString::number((qulonglong)pix);
        else return QString();
    }

public slots:
    void load(QString url) {
        QPixmap * old = 0;
        if (pix) old = pix;
        pix = new QPixmap(url);
        emit dataChanged();
        if (old) delete old;
    }

    void clear() {
        if (pix) delete pix;
        pix = 0;
        emit dataChanged();
    }

signals:
    void dataChanged();

private:
    QPixmap * pix;
};

Pixmap 元素的實(shí)現(xiàn)非常簡(jiǎn)單,雖然初始的有點(diǎn)受限,因?yàn)樾碌南袼貓D恰好被分配在與 data 不同圖像的字符串相同,導(dǎo)致QML圖像組件不更新,但解決方案很簡(jiǎn)單,只有在分配新像素圖后才刪除舊像素圖.這是實(shí)際的圖像提供程序:

The implementation of the Pixmap element is pretty straightforward, although the initial was a bit limited, since the new pixmap happened to be allocated at the exactly same memory address the data string was the same for different images, causing the QML Image component to not update, but the solution was as simple as deleting the old pixmap only after the new one has been allocated. Here is the actual image provider:

class PixmapProvider : public QQuickImageProvider {
public:
    PixmapProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {}
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) {
        qulonglong d = id.toULongLong();
        if (d) {
            QPixmap * p = reinterpret_cast<QPixmap *>(d);
            return *p;
        } else {
            return QPixmap();
        }
    }
};

注冊(cè):

//in main()
engine.addImageProvider("pixmap", new PixmapProvider);
qmlRegisterType<Pixmap>("Test", 1, 0, "Pixmap");

這就是你在 QML 中使用它的方式:

And this is how you use it in QML:

Pixmap {
    id: pix
}

Image {
    source: pix.data
}

// and then pix.load(path)

還請(qǐng)注意,在我的情況下,沒有實(shí)際修改需要在 QML 中更新的像素圖.如果在 C++ 中更改圖像,此解決方案不會(huì)自動(dòng)更新 QML 中的圖像,因?yàn)閮?nèi)存中的地址將保持不變.但解決方案同樣簡(jiǎn)單——實(shí)現(xiàn)一個(gè) update() 方法來分配一個(gè) new QPixmap(oldPixmap)——它將使用相同的內(nèi)部數(shù)據(jù),但會(huì)給你一個(gè)具有新內(nèi)存地址的新訪問器,這將觸發(fā) QML 圖像更新更改.這意味著提供的訪問像素圖的方法將通過 Pixmap 類,而不是直接從 QPixmap * 中獲取,因?yàn)槟鷮⑿枰?Pixmap 類要觸發(fā) data 更改,只需為 pix 添加一個(gè)訪問器,以防萬一您執(zhí)行復(fù)雜或線程化的操作,您可能需要使用 QImage 而是添加一個(gè)互斥鎖,以便在 QML 中底層數(shù)據(jù)在 C++ 中或其他方式中更改時(shí)不會(huì)更改.

ALSO Note that in my case there was no actual modification of the pixmap that needed to be updated in QML. This solution will not auto-update the image in QML if it is changed in C++, because the address in memory will stay the same. But the solution for this is just as straightforward - implement an update() method that allocates a new QPixmap(oldPixmap) - it will use the same internal data but give you a new accessor to it with a new memory address, which will trigger the QML image to update on changes. This means the proffered method to access the pixmap will be through the Pixmap class, not directly from the QPixmap * since you will need the Pixmap class to trigger the data change, so just add an accessor for pix, and just in case you do complex or threaded stuff, you might want to use QImage instead and add a mutex so that the underlying data is not changed in QML while being changed in C++ or the other way around.

這篇關(guān)于QML 和 C++ 圖像互操作性的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产精品久久国产精品久久 | www.99热| 免费的av网站 | 久久久久亚洲国产| 亚洲欧美日韩中文在线 | 中文字幕免费观看 | 特级生活片 | 国产精品久久久久久久久免费相片 | 国产在线看片 | 欧美一区二区在线观看 | 三区在线观看 | 国产精品久久久亚洲 | 亚洲免费在线观看 | 日韩免费一区 | 手机在线观看av | 久久亚洲一区二区三区四区 | 交专区videossex农村 | 精品乱人伦一区二区三区 | 观看av| 久久久久国产精品www | 国产一区二区三区高清 | 亚洲+变态+欧美+另类+精品 | 亚洲精品永久免费 | 最新免费黄色网址 | 中文字幕91 | 成人精品一区二区 | 久久这里只有精品首页 | 欧美三区 | 国产一区不卡在线观看 | 欧美在线播放一区 | 久久综合99 | 二区三区视频 | 2022精品国偷自产免费观看 | 日韩一区二区在线视频 | 亚洲成人一区 | 日韩不卡在线 | 超碰美女在线 | 亚洲毛片在线 | 91精品国产一区二区三区 | 亚洲精品乱码久久久久久蜜桃 | 国产精品1区2区 |