Home > other >  From QQmlListProperty QML passed as a parameter C
From QQmlListProperty QML passed as a parameter C

Time:05-25

I have a QML ProviderItem with objects property returns a QObject derived object list. I want to use this list as a function of consumeAll properties passed to another QML ConsumerItem. The problem is that I always get empty QQmlListProperty, what?? A callback function is set to 0, the data pointer is set to 0 (I think these are the default structure of value)
Something like this:
ProviderItem. H

The class ProviderItem: public QObject, public QQmlParserStatus
{
Q_OBJECT
Public:
Q_PROPERTY (QQmlListProperty Objects READ objects NOTIFY objectsChanged)

QQmlListProperty Objects ();
Static int objects_count (QQmlListProperty *);
The static QObject * objects_at (QQmlListProperty *, int);

Private:
QList}

ProviderItem. CPP

QQmlListProperty ProviderItemPrivate: : objects ()
{
Return QQmlListProperty (this, nullptr,
ProviderItem: : objects_count,
ProviderItem: : objects_at);
}

QObject * ProviderItem: : objects_at (QQmlListProperty * prop, int index)
{
ProviderItem * provider=qobject_cast & lt; ProviderItem * & gt; (prop - & gt; Object)
Return the provider - & gt; M_objects. At (index);
}

Int ProviderItem: : objects_count (QQmlListProperty * prop)
{
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 Obj);
};

ConsumerItem. CPP

Void ConsumerItem: : consumeAll (QQmlListProperty Obj)
{
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 (uri, major, minor, "Provider");
QmlRegisterType (uri, major, minor, "Consumer");

I also tried:

Q_INVOKABLE void consumeAll (QVariantMap obj);
Q_INVOKABLE void consumeAll (QQmlListProperty Obj);
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 Products READ products NOTIFY productsChanged)
Public:
Using QObject: : QObject;
QQmlListProperty Products () {
Return QQmlListProperty (this, this,
& 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
  • Related