最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

一、設(shè)置簡單的上下文屬性

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

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


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

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


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

二、將對象設(shè)置為上下文屬性

上下文屬性可以包含 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() 和相關(guān)函數(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 }
}

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

相關(guān)文章

最新評論

廉江市| 平阳县| 孙吴县| 兴文县| 晋宁县| 祥云县| 永清县| 和平县| 大悟县| 龙泉市| 陈巴尔虎旗| 河津市| 慈溪市| 武强县| 翁牛特旗| 新乡县| 准格尔旗| 乌兰察布市| 清水河县| 清涧县| 五家渠市| 丹江口市| 海宁市| 安溪县| 海淀区| 合肥市| 萝北县| 阿巴嘎旗| 大渡口区| 巴林右旗| 黄骅市| 萨迦县| 鸡泽县| 宜春市| 嵊州市| 马公市| 喀喇沁旗| 津南区| 金昌市| 古浪县| 奈曼旗|