pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

QML 和 C++ 圖像互操作性

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

問題描述

我已經瀏覽了文檔以及我可以在互聯網上找到的所有內容,但似乎無法從 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++.

有沒有辦法解決這個問題?

Is there a way to work around that?

推薦答案

在 QtQuick1 中可以實現,但在 QtQuick2 中刪除了該功能.

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

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

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 元素的實現非常簡單,雖然初始的有點受限,因為新的像素圖恰好被分配在與 data 不同圖像的字符串相同,導致QML圖像組件不更新,但解決方案很簡單,只有在分配新像素圖后才刪除舊像素圖.這是實際的圖像提供程序:

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

注冊:

//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)

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

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.

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

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 成都网站建设制作_高端网站设计公司「做网站送优化推广」 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 铜镍-康铜-锰铜-电阻合金-NC003 - 杭州兴宇合金有限公司 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 九州网址_专注于提供网址大全分享推广中文网站导航服务 | pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | 中药超微粉碎机(中药细胞级微粉碎)-百科 | 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 苹果售后维修点查询,苹果iPhone授权售后维修服务中心 – 修果网 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 电缆接头_防水接头_电缆防水接头_防水电缆接头_上海闵彬 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 洗地机-全自动/手推式洗地机-扫地车厂家_扬子清洁设备 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 河南凯邦机械制造有限公司| 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 手持式3d激光扫描仪-便携式三维立体扫描仪-北京福禄克斯 | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 铁盒_铁罐_马口铁盒_马口铁罐_铁盒生产厂家-广州博新制罐 | 氨水-液氨-工业氨水-氨水生产厂家-辽宁顺程化工 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 手板_手板模型制作_cnc手板加工厂-东莞天泓| 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 洛阳装修公司-洛阳整装一站式品牌-福尚云宅装饰 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 洁净化验室净化工程_成都实验室装修设计施工_四川华锐净化公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | 户外-组合-幼儿园-不锈钢-儿童-滑滑梯-床-玩具-淘气堡-厂家-价格 | 圆窗水平仪|伊莉莎冈特elesa+ganter| 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 |