[英]Pass QQmlListProperty from QML to C++ as parameter
I have a QML ProviderItem that has objects property returning list of QObject-derived objects. 我有一个QML ProviderItem,它有objects属性返回QObject派生对象的列表。
I want to pass this list to another QML ConsumerItem as property for its function consumeAll. 我想将此列表作为其函数consumeAll的属性传递给另一个QML ConsumerItem。 The problem is that I always get empty QQmlListProperty with all callback functions set to 0 and data pointer set to 0 (I think these are default-constructed values) 问题是我总是得到空的QQmlListProperty,所有回调函数都设置为0,数据指针设置为0(我认为这些是默认构造的值)
Something like this: 像这样的东西:
ProviderItem.h ProviderItem.h
class ProviderItem : public QObject, public QQmlParserStatus
{
Q_OBJECT
public:
Q_PROPERTY(QQmlListProperty<QObject> objects READ objects NOTIFY objectsChanged)
QQmlListProperty<QObject> objects();
static int objects_count(QQmlListProperty<QObject> *);
static QObject* objects_at(QQmlListProperty<QObject> *, int);
private:
QList<QObject*> m_objects;
}
ProviderItem.cpp ProviderItem.cpp
QQmlListProperty<QObject> ProviderItemPrivate::objects()
{
return QQmlListProperty<QObject>(this, nullptr,
ProviderItem::objects_count,
ProviderItem::objects_at);
}
QObject* ProviderItem::objects_at(QQmlListProperty<QObject> *prop, int index)
{
ProviderItem* provider = qobject_cast<ProviderItem*>(prop->object)
return provider->m_objects.at(index);
}
int ProviderItem::objects_count(QQmlListProperty<QObject> *prop)
{
ProviderItem* provider = qobject_cast<ProviderItem*>(prop->object)
return provider->m_objects.count();
}
ConsumerItem.h ConsumerItem.h
class ConsumerItem: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void consumeAll(QQmlListProperty<QObject> obj);
};
ConsumerItem.cpp ConsumerItem.cpp
void ConsumerItem::consumeAll(QQmlListProperty<QObject> obj)
{
qDebug() << obj.count(); // thows exeption as count callback is 0
}
main.qml main.qml
Provider {
id: objectProvider
}
Consumer {
id: objectConsumer
}
Connections {
target: objectProvider
onObjectsChanged: {
console.debug(objectProvider.objects) // gives [object Object]
objectConsumer.consumeAll(objectProvider.objects)
var test = objectProvider.objects
console.debug(test) // gives [object Object]
Thermonav.testList(objectProvider.objects)
}
}
Obviously ProviderItem and ConsumerItem are registered: 显然,ProviderItem和ConsumerItem已注册:
main.cpp main.cpp中
qmlRegisterType<ProviderItem>(uri, major, minor, "Provider");
qmlRegisterType<ConsumerItem>(uri, major, minor, "Consumer");
I have also tried: 我也尝试过:
Q_INVOKABLE void consumeAll(QVariantMap obj);
Q_INVOKABLE void consumeAll(QQmlListProperty<QObject> obj);
Q_INVOKABLE void consumeAll(void* p);
Q_INVOKABLE void consumeAll(QVariant p);
but every time I get default-costructed values. 但每次我得到默认构造的值。
According to this article : 根据这篇文章 :
When integrating with C++, note that any QQmlListProperty value passed into QML from C++ is automatically converted into a list value, and vice-versa. 与C ++集成时,请注意从C ++传递到QML的任何QQmlListProperty值都会自动转换为列表值,反之亦然。
so output in qml [object Object] looks legit to me, as "list" is not js data type. 所以qml [object Object]中的输出对我来说是合法的,因为“list”不是js数据类型。 But it also says that QML list should be converted back in QQmlListProperty that definitely not working for me (or I am doing it wrong). 但它也说QML列表应该在QQmlListProperty中转换回来,这绝对不适合我(或者我做错了)。
I am using Qt 5.12.0 我使用的是Qt 5.12.0
So how do I pass QQmlListProperty created in C++ to QML list and then to QQmlListProperty in C++? 那么如何将用C ++创建的QQmlListProperty传递给QML列表然后传递给C ++中的QQmlListProperty呢?
If you use QVariant
and print: 如果您使用QVariant
并打印:
class ConsumerItem: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void consumeAll(QVariant objects){
qDebug() << objects;
}
};
You get: 你得到:
QVariant(QQmlListReference, )
So the solution is to use QQmlListReference
: 所以解决方法是使用QQmlListReference
:
class ConsumerItem: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void consumeAll(const QQmlListReference & objects){
qDebug() << objects.count();
}
};
Complete code: 完整代码:
main.cpp main.cpp中
#include <QtQml>
#include <QtGui>
class Product: public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
Product(const QString & name="", QObject* parent=nullptr):
QObject(parent), m_name(name){}
QString name() const{return m_name;}
void setName(const QString &name){
if(m_name == name) return;
m_name = name;
Q_EMIT nameChanged(m_name);
}
Q_SIGNAL void nameChanged(const QString &);
private:
QString m_name;
};
class ProviderItem: public QObject{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<Product> products READ products NOTIFY productsChanged)
public:
using QObject::QObject;
QQmlListProperty<Product> products(){
return QQmlListProperty<Product>(this, this,
&ProviderItem::appendProduct,
&ProviderItem::productCount,
&ProviderItem::product,
&ProviderItem::clearProducts);
}
void appendProduct(Product* p) {
m_products.append(p);
Q_EMIT productsChanged();
}
int productCount() const{return m_products.count();}
Product *product(int index) const{ return m_products.at(index);}
void clearProducts() {
m_products.clear();
Q_EMIT productsChanged();
}
Q_SIGNAL void productsChanged();
private:
static void appendProduct(QQmlListProperty<Product>* list, Product* p) {
reinterpret_cast<ProviderItem* >(list->data)->appendProduct(p);
}
static void clearProducts(QQmlListProperty<Product>* list) {
reinterpret_cast<ProviderItem* >(list->data)->clearProducts();
}
static Product* product(QQmlListProperty<Product>* list, int i) {
return reinterpret_cast<ProviderItem* >(list->data)->product(i);
}
static int productCount(QQmlListProperty<Product>* list) {
return reinterpret_cast<ProviderItem* >(list->data)->productCount();
}
QVector<Product *> m_products;
};
class ConsumerItem: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void consumeAll(const QQmlListReference & products){
for(int i=0; i<products.count(); ++i){
if(Product *product = qobject_cast<Product *>(products.at(i))){
qDebug()<< product->name();
}
}
}
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<Product>("foo", 1, 0, "Product");
qmlRegisterType<ProviderItem>("foo", 1, 0, "Provider");
qmlRegisterType<ConsumerItem>("foo", 1, 0, "Consumer");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
#include "main.moc"
main.qml main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import foo 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
function create_product(){
var product = Qt.createQmlObject('import foo 1.0; Product {}',
provider,
"dynamicSnippet1");
product.name = "product"+provider.products.length;
provider.products.push(product)
}
Timer {
interval: 1000; running: true; repeat: true
onTriggered: create_product()
}
Provider{
id: provider
onProductsChanged: consumer.consumeAll(provider.products)
products: [
Product{name: "product0"},
Product{name: "product1"},
Product{name: "product2"}
]
}
Consumer{
id: consumer
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.