Pass an array of objects to c++ from qml
-
wrote on 1 Apr 2018, 10:01 last edited by
Trying to pass an array of objects to c++ from qml, but cannot find way to do it properly.
qml sites
function getCheckedAudioObject() { var results = [] for(var i = 0; i < checkSongIndex.length; ++i){ var obj = audioModel.get(checkSongIndex[i]) //audioModel is a ListModel results.push(obj) } return results }
c++ api
Q_INVOKABLE void addAudioInformation(QVariantList const &audio_info) { qDebug()<<__func__<<audio_info.size(); //print 4, as expected qDebug()<<__func__<<audio_info[0].typeName(); //print QObject* qDebug()<<__func__<<audio_info[0].toMap(); //print Map() }
How could I get the data of qml object from c++ api?Thanks
-
Trying to pass an array of objects to c++ from qml, but cannot find way to do it properly.
qml sites
function getCheckedAudioObject() { var results = [] for(var i = 0; i < checkSongIndex.length; ++i){ var obj = audioModel.get(checkSongIndex[i]) //audioModel is a ListModel results.push(obj) } return results }
c++ api
Q_INVOKABLE void addAudioInformation(QVariantList const &audio_info) { qDebug()<<__func__<<audio_info.size(); //print 4, as expected qDebug()<<__func__<<audio_info[0].typeName(); //print QObject* qDebug()<<__func__<<audio_info[0].toMap(); //print Map() }
How could I get the data of qml object from c++ api?Thanks
@tham said in Pass an array of objects to c++ from qml:
qDebug()<<__func__<<audio_info[0].typeName(); //print QObject*
The element is a
QObject*
.qDebug()<<__func__<<audio_info[0].toMap(); //print Map()
You cannot convert a
QObject*
to aQMap
.First, what does
qDebug() << audio_info[0];
print? Is it your own custom QObject? If so, you can add a conversion function to the object.Otherwise, you can create a JavaScript object in QML and put this in your array. JavaScript objects can be treated as
QVariantMap
in C++:var results = [] for(var i = 0; i < checkSongIndex.length; ++i){ var qObj = audioModel.get(checkSongIndex[i]) var jObj = {} jObj['property1'] = qObj.func1() // Your own function to extract data from the model's element jObj['property2'] = qObj.func2() results.push(jObj) }
-
@tham said in Pass an array of objects to c++ from qml:
qDebug()<<__func__<<audio_info[0].typeName(); //print QObject*
The element is a
QObject*
.qDebug()<<__func__<<audio_info[0].toMap(); //print Map()
You cannot convert a
QObject*
to aQMap
.First, what does
qDebug() << audio_info[0];
print? Is it your own custom QObject? If so, you can add a conversion function to the object.Otherwise, you can create a JavaScript object in QML and put this in your array. JavaScript objects can be treated as
QVariantMap
in C++:var results = [] for(var i = 0; i < checkSongIndex.length; ++i){ var qObj = audioModel.get(checkSongIndex[i]) var jObj = {} jObj['property1'] = qObj.func1() // Your own function to extract data from the model's element jObj['property2'] = qObj.func2() results.push(jObj) }
wrote on 5 Apr 2018, 14:04 last edited by@JKSH said in Pass an array of objects to c++ from qml:
Is it your own custom QObject?
No, it is ListModel of qml.
-
@JKSH said in Pass an array of objects to c++ from qml:
Is it your own custom QObject?
No, it is ListModel of qml.
@tham said in Pass an array of objects to c++ from qml:
@JKSH said in Pass an array of objects to c++ from qml:
Is it your own custom QObject?
No, it is ListModel of qml.
Then you must convert each audioModel element to a custom JavaScript object. Pass an array of your custom JavaScript objects to C++.
1/4