欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用上下文屬性將?C++?對象嵌入?QML?里

 更新時間:2022年01月24日 17:20:12   作者:友善啊,朋友  
這篇文章主要介紹了利用上下文屬性將?C++?對象嵌入?QML里,將?QML?對象加載到?C++?應用程序中時,直接嵌入一些可在?QML?代碼中使用的?C++?數(shù)據(jù)會很有用。例如,這使得在嵌入對象上調(diào)用?C++?方法或使用?C++?對象實例作為?QML?視圖的數(shù)據(jù)模型成為可能,下面一起來學習該內(nèi)容吧

QQmlContext 類使將 C++ 數(shù)據(jù)注入 QML 對象的能力成為可能。此類向 QML 對象的上下文公開數(shù)據(jù),以便可以直接從 QML 代碼范圍內(nèi)引用數(shù)據(jù)。

一、設置簡單的上下文屬性

例如,這里有一個 QML 項,它引用了當前作用域中不存在的 currentDateTime 值:

// MyItem.qml
import QtQuick 2.0
 
Text 
{ 
    text: currentDateTime 
}


這個值可以由加載 QML 組件的 C++ 應用程序使用 QQmlContext::setContextProperty() 直接設置:

    QQuickView view;
    view.rootContext()->setContextProperty("currentDateTime",QDateTime::currentDateTime());
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();


由于在 QML 中計算的所有表達式都是在特定上下文中計算的,如果修改了上下文,則將重新計算該上下文中的所有綁定。因此,應在應用程序初始化之外謹慎使用上下文屬性,因為這可能會導致應用程序性能下降。

二、將對象設置為上下文屬性

上下文屬性可以包含 QVariant QObject* 值。 這意味著也可以使用這種方法注入自定義 C++ 對象,并且可以直接在 QML 中修改和讀取這些對象。修改上面的例子,嵌入一個 QObject 實例而不是一個 QDateTime 值,QML 代碼在對象實例上調(diào)用一個方法:

class ApplicationData : public QObject
{
    Q_OBJECT
public:
    Q_INVOKABLE QDateTime getCurrentDateTime() const 
    {
        return QDateTime::currentDateTime();
    }
};
 
int main(int argc, char *argv[]) 
{
    QGuiApplication app(argc, argv);
 
    ApplicationData data;
 
    QQuickView view;
    view.rootContext()->setContextProperty("applicationData", &data);
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();
 
    return app.exec();
}
// MyItem.qml
import QtQuick 2.0
 
Text 
{ 
    text: applicationData.getCurrentDateTime() 
}


請注意:從 C++ 返回到 QML 的日期/時間值可以通過 Qt.formatDateTime() 和相關函數(shù)進行格式化。

如果 QML 項需要從上下文屬性接收信號,它可以使用 Connections 類型連接到它們。 例如,如果 ApplicationData 有一個名為 dataChanged() 的信號,則可以使用 Connections 對象中的 onDataChanged 處理程序連接到該信號:

Text 
{
    text: applicationData.getCurrentDateTime()
 
    Connections 
    {
        target: applicationData
        onDataChanged: console.log("The application data changed!")
    }
}

三、上下文屬性與C++ 的數(shù)據(jù)模型示例

3.1、字符串列表模型

int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
 
    QStringList dataList;
    dataList.append("Item 1");
    dataList.append("Item 2");
    dataList.append("Item 3");
    dataList.append("Item 4");
 
    QQuickView view;
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
 
    view.setSource(QUrl("qrc:view.qml"));
    view.show();
 
    return app.exec();
}


import QtQuick 2.0
 
ListView 
{
    width: 100; height: 100
 
    model: myModel
    delegate: Rectangle 
    {
        height: 25
        width: 100
        Text { text: modelData }
    }
}

3.2、對象列表模型

#ifndef DATAOBJECT_H
#define DATAOBJECT_H
 
#include <QObject>
 
class DataObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
public:
    DataObject(QObject *parent=nullptr);
    DataObject(const QString &name, const QString &color, QObject *parent=nullptr);
 
    QString name() const;
    void setName(const QString &name);
 
    QString color() const;
    void setColor(const QString &color);
 
signals:
    void nameChanged();
    void colorChanged();
 
private:
    QString m_name;
    QString m_color;
};
 
#endif // DATAOBJECT_H


#include <QDebug>
#include "dataobject.h"
 
DataObject::DataObject(QObject *parent)
    : QObject(parent)
{
}
 
DataObject::DataObject(const QString &name, const QString &color, QObject *parent)
    : QObject(parent), m_name(name), m_color(color)
{
}
 
QString DataObject::name() const
{
    return m_name;
}
 
void DataObject::setName(const QString &name)
{
    if (name != m_name) 
    {
        m_name = name;
        emit nameChanged();
    }
}
 
QString DataObject::color() const
{
    return m_color;
}
 
void DataObject::setColor(const QString &color)
{
    if (color != m_color) 
    {
        m_color = color;
        emit colorChanged();
    }
}
#include "dataobject.h"
 
int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
 
    QList<QObject*> dataList;
    dataList.append(new DataObject("Item 1", "red"));
    dataList.append(new DataObject("Item 2", "green"));
    dataList.append(new DataObject("Item 3", "blue"));
    dataList.append(new DataObject("Item 4", "yellow"));
 
    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
 
    view.setSource(QUrl("qrc:view.qml"));
    view.show();
 
    return app.exec();
}
import QtQuick 2.0
 
ListView 
{
    width: 100; height: 100
 
    model: myModel
    delegate: Rectangle 
    {
        height: 25
        width: 100
        color: model.modelData.color
        Text { text: name }
    }
}

3.3、QAbstractItemModel

#include <QAbstractListModel>
#include <QStringList>
 
class Animal
{
public:
    Animal(const QString &type, const QString &size);
    QString type() const;
    QString size() const;
 
private:
    QString m_type;
    QString m_size;
};
 
class AnimalModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum AnimalRoles
    {
        TypeRole = Qt::UserRole + 1,
        SizeRole
    };
 
    AnimalModel(QObject *parent = nullptr);
    void addAnimal(const Animal &animal);
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
 
protected:
    QHash<int, QByteArray> roleNames() const;
 
private:
    QList<Animal> m_animals;
};


#include "model.h"
 
Animal::Animal(const QString &type, const QString &size)
    : m_type(type), m_size(size)
{
}
 
QString Animal::type() const
{
    return m_type;
}
 
QString Animal::size() const
{
    return m_size;
}
 
AnimalModel::AnimalModel(QObject *parent)
    : QAbstractListModel(parent)
{
}
 
void AnimalModel::addAnimal(const Animal &animal)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_animals << animal;
    endInsertRows();
}
 
int AnimalModel::rowCount(const QModelIndex & parent) const
{
    Q_UNUSED(parent)
    return m_animals.count();
}
 
QVariant AnimalModel::data(const QModelIndex & index, int role) const
{
    if (index.row() < 0 || index.row() >= m_animals.count())
        return QVariant();
 
    const Animal &animal = m_animals[index.row()];
    if (role == TypeRole)
        return animal.type();
    else if (role == SizeRole)
        return animal.size();
    return QVariant();
}
 
QHash<int, QByteArray> AnimalModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[SizeRole] = "size";
    return roles;
}
int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
 
    AnimalModel model;
    model.addAnimal(Animal("Wolf", "Medium"));
    model.addAnimal(Animal("Polar bear", "Large"));
    model.addAnimal(Animal("Quoll", "Small"));
 
    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", &model);
 
    view.setSource(QUrl("qrc:view.qml"));
    view.show();
 
    return app.exec();
}

import QtQuick 2.0
 
ListView
{
    width: 200; height: 250
 
    model: myModel
    delegate: Text { text: "Animal: " + type + ", " + size }
}

到此這篇關于利用上下文屬性將 C++ 對象嵌入 QML 里的文章就介紹到這了,更多相關  C++ 對象嵌入 QML 里內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于Matlab LBP實現(xiàn)植物葉片識別功能

    基于Matlab LBP實現(xiàn)植物葉片識別功能

    局部二值模式(LBP)是由Ojala等人于2002年提出,它被用于特征提取,而且提取的特征是圖像的紋理特征。本文將利用Matlab和LBP實現(xiàn)植物葉片識別,需要的可以參考一下
    2022-02-02
  • libevent庫的使用方法實例

    libevent庫的使用方法實例

    這篇文章主要介紹了libevent庫的使用方法實例,有需要的朋友可以參考一下
    2013-12-12
  • C++實現(xiàn)基于控制臺界面的吃豆子游戲

    C++實現(xiàn)基于控制臺界面的吃豆子游戲

    這篇文章主要介紹了C++實現(xiàn)基于控制臺界面的吃豆子游戲,實例分析了吃豆子游戲的原理與C++實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C++中sort()函數(shù)和priority_queue容器中比較函數(shù)的區(qū)別詳析

    C++中sort()函數(shù)和priority_queue容器中比較函數(shù)的區(qū)別詳析

    C++中sort()和priority_queue都能自定義比較函數(shù),其中sort()自定義的比較函數(shù)比較好理解,priority_queue中自定義的比較函數(shù)的效果和sort()是相反的,這篇文章主要給大家介紹了關于C++中sort()函數(shù)和priority_queue容器中比較函數(shù)的區(qū)別的相關資料,需要的朋友可以參考下
    2023-03-03
  • C語言數(shù)據(jù)結構與算法之鏈表(一)

    C語言數(shù)據(jù)結構與算法之鏈表(一)

    鏈表是線性表的鏈式存儲方式。鏈表的內(nèi)存是不連續(xù)的,前一個元素存儲地址的下一個地址中存儲的不一定是下一個元素。小編今天就將帶大家深入了解一下鏈表,快來學習吧
    2021-12-12
  • C語言進階:指針的進階(3)

    C語言進階:指針的進階(3)

    這篇文章主要介紹了C語言指針詳解及用法示例,介紹了其相關概念,然后分享了幾種用法,具有一定參考價值。需要的朋友可以了解下
    2021-09-09
  • C++?容器?Vector?的使用方法

    C++?容器?Vector?的使用方法

    這篇文章主要介紹了C++?容器?Vector?的使用方法,Vector?是一個能夠存放任意類型的動態(tài)數(shù)組,有點類似數(shù)組,是一個連續(xù)地址空間,下文更多詳細內(nèi)容的介紹,需要的小伙伴可以參考一下
    2022-06-06
  • Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運行C、C++的教程詳解(Windows)【真正的小白版】

    Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運行C、C++的教程詳解(Windows

    這篇文章主要介紹了Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運行C、C++的教程詳解(Windows)【真正的小白版】,圖文詳解介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 淺析操作系統(tǒng)中的虛擬地址與物理地址

    淺析操作系統(tǒng)中的虛擬地址與物理地址

    本文主要介紹了操作系統(tǒng)中的虛擬地址與物理地址。在早期的計算機中,要運行一個程序,會把這些程序全都裝入內(nèi)存,程序都是直接運行在內(nèi)存上的,也就是說程序中訪問的內(nèi)存地址都是實際的物理內(nèi)存地址。那當程序同時運行多個程序時,操作系統(tǒng)是如何為這些程序分配內(nèi)存的呢
    2021-06-06
  • C++中指向結構體變量的指針

    C++中指向結構體變量的指針

    結構體變量的指針就是該變來那個所占據(jù)的內(nèi)存段的起始地址??梢栽O一個指針變量,來指向一個結構體變量,此時該指針變量的值是結構體變量的起始地址
    2013-10-10

最新評論