本文向您展示了如何使用上下文属性将C对象嵌入到QML中。内容简洁易懂,一定会让你大放异彩。希望通过这篇文章的详细介绍,你能有所收获。
一、设置简单的上下文属性
例如,这里有一个 QML 项,它引用了当前作用域中不存在的 currentDateTime 值:
//MyItem.qml
importQtQuick2.0
文本
{
文本:当前日期时间
}该值可以由使用qmlcontext :3360 SetContextProperty()加载QML组件的C应用程序直接设置:
QQuickViewview
view . rootcontext()-set context property(' current datetime ',qdatetime : current datetime());
view . setsource(qurl: from local file(' myitem . qml '));
view . show();由于在QML评估的所有表达式都是在特定的上下文中评估的,因此如果修改了上下文,将会重新计算该上下文中的所有绑定。因此,应该在应用程序初始化之外小心使用上下文属性,因为它可能会导致应用程序的性能下降。
00-1010上下文属性可以包含QVariant或QObject*值。这意味着这个方法也可以用来注入自定义的C对象,这些对象可以在QML直接修改和读取。修改上面的示例,嵌入一个QObject实例而不是一个QDateTime值,QML代码在对象实例上调用一个方法:
class applicationdata : public qobject
{
Q_OBJECT
公众号:
q _ invokableeqdatetime getcurrentdatetime()常量
{
returnqdatetime : current datetime();
}
};
intmain(intargc,char*argv[])
{
QGuiApplicationapp(argc,argv);
ApplicationDatadata
QQuickViewview
view . rootcontext()-setContextProperty(' application data ',data);
view . setsource(qurl: from local file(' myitem . qml '));
view . show();
return PP . exec();
}
//MyItem.qml
importQtQuick2.0
文本
{
text : application data . getcurrentdatetime()
}请注意,从C返回到QML的日期/时间值可以通过Qt.formatDateTime()和相关函数进行格式化。
如果QML项需要从上下文属性接收信号,它可以使用连接类型来连接它们。例如,如果应用程序
nData 有一个名为 dataChanged()
的信号,则可以使用 Connections
对象中的 onDataChanged
处理程序连接到该信号:
Text { text: applicationData.getCurrentDateTime() Connections { target: applicationData onDataChanged: console.log("The application data changed!") } }
三、上下文属性与C++ 的数据模型示例
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 里,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/147369.html