[Solved]How to pass QModelIndex from QML to C++
-
Hello,
I have an C++ based application which exposed QAbstractmodel to QML file as data model.I want to get the Qmodelindex from list view. so that why i set c++ model as visualdata model & visual data model as listmodel as following
@
ListView
{
id: listView
anchors.fill: parent
model:VisualDataModel
{
model: cppModel //cppModel is model exposed from c++ file
delegate: petDelegate
}}
@
after that i want to get model index & pass it to c++. for that i write code as..
@
mainView.somesignal(listView.model.modelIndex(index))
@
but when i print modelindex's value it print as: QVariant(QModelIndex)So How can get the value of model index in QML??
Edit: please wrap your code snippets with @ tags; Andre
-
If your problem is that the value you get is a QVariant instead of a QModelIndex, use the following on the C++ side to convert it back:
@
QModelIndex qmIndex = index.value<QModelIndex>();
@ -
Actually i am using Christophe Dumez blog "(http://cdumez.blogspot.in/2010/11/how-to-use-c-list-model-in-qml.html)":http:// in my application. In qml i want that when i click on the list item, then it should return model index of that item so that i can take action based on that. in c++ code i capture this signal and based on modelindex i add/remove item in/from another list. for that in qml i write code as
@
signal blockUri(variant uri)//in mainviewListView
{
id: InfoView
anchors.fill: parent
model:VisualDataModel
{
model: watcherInfoModel
delegate: petDelegate
}
}onClicked{
mainView.blockUri(InfoView.model.modelIndex(index))//blockUri is signal & mainView is id of main view}
@on c++ side i print value of of QVariant, is display as
QVariant(QModelIndex, )and in QML it display as
QVariant(QModelIndex)when i try to convert Qvariant inot Qmodelindex by using
@QModelIndex qmIndex = index.value<QModelIndex>();@there is error like
@c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtcore\qmetatype.h:169: error: C2039: 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>'
with
[
T=QModelIndex
]@so how can get model index of item. Is there any other way to get modelindex??
-
On the c++ side, try adding
@
#include <QAbstractItemModel>
@... so the QModelIndex type is recognized.
If it doesn't work try adding this too@
Q_DECLARE_METATYPE(QModelIndex)
@ -
Thank you. Now its working. i was missing to add @Q_DECLARE_METATYPE(QModelIndex)@
-
You're welcome ! Don't forget to mark your thread [SOLVED] :)
-
Actually i am new in this forum. So i don't know how to mark it as solved. Can you please tell me how to mark it as solved?
-
Of course: Simply edit your first message (edit button near you avatar), and prepend [SOLVED] to the title of your thread. You can also add the "solved" tag to the thread tags (at the right of your first post), I did this.