Something like this:
ProviderItem. H
The class ProviderItem: public QObject, public QQmlParserStatus
{
Q_OBJECT
Public:
Q_PROPERTY (QQmlListProperty
QQmlListProperty
Static int objects_count (QQmlListProperty
The static QObject * objects_at (QQmlListProperty
Private:
QList
ProviderItem. CPP
QQmlListProperty
{
Return QQmlListProperty
ProviderItem: : objects_count,
ProviderItem: : objects_at);
}
QObject * ProviderItem: : objects_at (QQmlListProperty
{
ProviderItem * provider=qobject_cast & lt; ProviderItem * & gt; (prop - & gt; Object)
Return the provider - & gt; M_objects. At (index);
}
Int ProviderItem: : objects_count (QQmlListProperty
{
ProviderItem * provider=qobject_cast & lt; ProviderItem * & gt; (prop - & gt; Object)
Return the provider - & gt; M_objects. The count ();
}
ConsumerItem. H
The class ConsumerItem: public QObject
{
Q_OBJECT
Public:
Q_INVOKABLE void consumeAll (QQmlListProperty
};
ConsumerItem. CPP
Void ConsumerItem: : consumeAll (QQmlListProperty
{
QDebug () & lt;
. The main QML
The Provider {
Id: objectProvider
}
Consumer {
Id: objectConsumer
}
Connections {
Target: objectProvider
OnObjectsChanged: {
The console. The debug (objectProvider. Objects)//gives [object object]
ObjectConsumer. ConsumeAll (objectProvider. Objects)
Var test=objectProvider. Objects
The console. The debug (test)//gives [object object]
Thermonav. TestList (objectProvider. Objects)
}
}
Obviously, ProviderItem and ConsumerItem registered:
In the main. CPP
QmlRegisterType
QmlRegisterType
I also tried:
Q_INVOKABLE void consumeAll (QVariantMap obj);
Q_INVOKABLE void consumeAll (QQmlListProperty
Q_INVOKABLE void consumeAll (void * p);
Q_INVOKABLE void consumeAll QVariant (p);
But every time I get the default value of the structure.
According to this article:
The When integrating with c + +, note that any QQmlListProperty value passed into QML from c + + is automatically converted into a list value, and vice - versa.
So QML [object object] the output is legal for me, because the "a list" is not js data type. But it is also said to QML list should be back in QQmlListProperty transformation, it is not suitable for me (or did I do wrong.)
I use a Qt 5.12.0
So how do you pass C created in QQmlListProperty QML QQmlListProperty in a list and then passed to the C?
CodePudding user response:
If you are using QVariant and print:The class ConsumerItem: public QObject
{
Q_OBJECT
Public:
Using QObject: : QObject;
Q_INVOKABLE void consumeAll (QVariant objects) {
QDebug () & lt;
};
You get:
QVariant (QQmlListReference,)
So the solution is to use QQmlListReference:
The class ConsumerItem: public QObject
{
Q_OBJECT
Public:
Using QObject: : QObject;
Q_INVOKABLE void consumeAll (const QQmlListReference & amp; Objects) {
QDebug () & lt;
};
The complete code:
In the main. CPP
# include & lt; QtQml>
# include & lt; QtGui>
The class of the Product: public QObject {
Q_OBJECT
Q_PROPERTY (QString name READ name WRITE elegantly-named setName NOTIFY nameChanged)
Public:
The Product (const QString & amp; Name="", the parent QObject *=nullptr) :
QObject (parent), m_name (name) {}
Const QString name () {return m_name; }
Void elegantly-named setName (const QString & amp; Name) {
If (m_name==name) return;
M_name=name;
Q_EMIT nameChanged (m_name);
}
Q_SIGNAL void nameChanged (const QString & amp;);
Private:
QString m_name;
};
The class ProviderItem: public QObject {
Q_OBJECT
Q_PROPERTY (QQmlListProperty
Public:
Using QObject: : QObject;
QQmlListProperty
Return QQmlListProperty
& ProviderItem: : appendProduct,
& ProviderItem: : productCount,
& ProviderItem: : product,
& ProviderItem: : clearProducts);
}
Void appendProduct (Product * p) {
M_products. Append (p);
Q_EMIT productsChanged ();
}
Const int productCount () {return m_products. The count (); }
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull