[Solved] Invidual itemDelegate for each column of table\listview
-
@p3c0 Sorry for dig this up again, but how can i get the modelIndex of item in column in treeview?
With listview or tableview, i can get data from model with styleData.row, treeview has depth, which mean i will need a QModelIndex of parent to access data
-
Yeah, but sometime i want to get extra data from model, not just one data from column's role, since i create my own delegate for each column.
Return a QList or QMap instead of one solid value in get() from model is a work-around, get a QModelIndex() is more straight ahead, i think
-
Yeah, but sometime i want to get extra data from model, not just one data from column's role, since i create my own delegate for each column.
Return a QList or QMap instead of one solid value in get() from model is a work-around, get a QModelIndex() is more straight ahead, i think
-
You missing this const QModelIndex & parent = QModelIndex()
index(int row, int column, const QModelIndex & parent = QModelIndex()) const
The styleData.row in tableViewColumn return the row index of row RELATE to it parent.
However i don't know which row is it parent.Because of that, if you don't set QModelIndex in treeview.index(), it might lead to an app crash ( And i did try it before)
-
You missing this const QModelIndex & parent = QModelIndex()
index(int row, int column, const QModelIndex & parent = QModelIndex()) const
The styleData.row in tableViewColumn return the row index of row RELATE to it parent.
However i don't know which row is it parent.Because of that, if you don't set QModelIndex in treeview.index(), it might lead to an app crash ( And i did try it before)
@illunara Well ofcourse it is not the exact solution and hence I used the word "Similar". That solution was for
TableView
which doesn't require parent and it doesn't have parent-child like relationship.
In case ofTreeView
you have to include the third parameterparent
inindex
method which will then give you the exact child. But then finding the parent is again difficult IMO externally i.e from outside ofTreeView
. But you find it inside theTreeView
. For eg.TreeView { anchors.fill: parent model: dataModel TableViewColumn { title: "Title" role: "title" width: 320 delegate: Rectangle { color: "#E5E4E2" Text { text: styleData.value } MouseArea { anchors.fill: parent onClicked: console.log(styleData.index, dataModel.parent(styleData.index)) //gives the index of child and parent of that child } } } }
If you require it outside of
TreeView
may be you can try iterating thru parents and finding their children. -
@illunara Well ofcourse it is not the exact solution and hence I used the word "Similar". That solution was for
TableView
which doesn't require parent and it doesn't have parent-child like relationship.
In case ofTreeView
you have to include the third parameterparent
inindex
method which will then give you the exact child. But then finding the parent is again difficult IMO externally i.e from outside ofTreeView
. But you find it inside theTreeView
. For eg.TreeView { anchors.fill: parent model: dataModel TableViewColumn { title: "Title" role: "title" width: 320 delegate: Rectangle { color: "#E5E4E2" Text { text: styleData.value } MouseArea { anchors.fill: parent onClicked: console.log(styleData.index, dataModel.parent(styleData.index)) //gives the index of child and parent of that child } } } }
If you require it outside of
TreeView
may be you can try iterating thru parents and finding their children. -
@p3c0 There is no property styleData.index of TableViewColumn ( In the document, and i run a test as well).
-
@p3c0 So you mean that qt docs is missing information about TableViewColumn?
[http://doc.qt.io/qt-5/qml-qtquick-controls-tableviewcolumn.html#delegate-prop](link url)styleData.index only available in tableview, treeview, not TableViewColumn
And i did try,QVariantMap TreeViewDataModel::get(QModelIndex index) { qDebug() << (static_cast<ModelAbstract*>(index.internalPointer()) == nullptr); return QVariantMap(); }
The output is true, which mean QModelIndex is not valid, i guess.
-
@p3c0 So you mean that qt docs is missing information about TableViewColumn?
[http://doc.qt.io/qt-5/qml-qtquick-controls-tableviewcolumn.html#delegate-prop](link url)styleData.index only available in tableview, treeview, not TableViewColumn
And i did try,QVariantMap TreeViewDataModel::get(QModelIndex index) { qDebug() << (static_cast<ModelAbstract*>(index.internalPointer()) == nullptr); return QVariantMap(); }
The output is true, which mean QModelIndex is not valid, i guess.
styleData.index only available in tableview, treeview, not TableViewColumn
Since it is available in tableview and treeview it is also available in
TableViewColumn
. Aren't you usingTableViewColumn
inTreeView
?
What about this code ?MouseArea { anchors.fill: parent onClicked: console.log(styleData.index, dataModel.parent(styleData.index)) //gives the index of child and parent of that child }
Didn't this work for you ?
-
styleData.index only available in tableview, treeview, not TableViewColumn
Since it is available in tableview and treeview it is also available in
TableViewColumn
. Aren't you usingTableViewColumn
inTreeView
?
What about this code ?MouseArea { anchors.fill: parent onClicked: console.log(styleData.index, dataModel.parent(styleData.index)) //gives the index of child and parent of that child }
Didn't this work for you ?
@p3c0 I'm sorry about the late reply, thing is getting crazy here.
Anyway about the code you posted. Yes, i tried, but its not working. The styleData in tableviewColumn is difference one from Treeview styleData. That's why, you can't use styleData of tableview to access data in Treeview.The internalPointer of QModelIndex() using styleData.index in tableviewColumn is nullpointer
-
@p3c0 I'm sorry about the late reply, thing is getting crazy here.
Anyway about the code you posted. Yes, i tried, but its not working. The styleData in tableviewColumn is difference one from Treeview styleData. That's why, you can't use styleData of tableview to access data in Treeview.The internalPointer of QModelIndex() using styleData.index in tableviewColumn is nullpointer
-
@p3c0 I put the qml code in a gist since it quite long
https://gist.github.com/illunara/2957ae5d7e41befc77fbHere is the Constructor for TreeviewModel, i put an item inside it
TreeViewDataModel::TreeViewDataModel(QObject* parent ) : QAbstractItemModel(parent) { m_root = new Project(); Project* project = new Project(); project->m_name = "Hello World"; project->setParent(m_root); m_root->appendChild(project);
}
And this is the function i call from QML
QVariantMap TreeViewDataModel::get(QModelIndex index) { ModelAbstract* model = static_cast<ModelAbstract*>(index.internalPointer()); qDebug() << model->getData("ItemType"); return QVariantMap();
}
The result is
QVariant(QString, "Project") QVariant(QString, "Project")
This function got call twice, that's true ( because i have 2 items in treeview, one is the root, and other is the item i added) but result i wrong.
And if i add one more item under root, this will crash. -
@p3c0 I put the qml code in a gist since it quite long
https://gist.github.com/illunara/2957ae5d7e41befc77fbHere is the Constructor for TreeviewModel, i put an item inside it
TreeViewDataModel::TreeViewDataModel(QObject* parent ) : QAbstractItemModel(parent) { m_root = new Project(); Project* project = new Project(); project->m_name = "Hello World"; project->setParent(m_root); m_root->appendChild(project);
}
And this is the function i call from QML
QVariantMap TreeViewDataModel::get(QModelIndex index) { ModelAbstract* model = static_cast<ModelAbstract*>(index.internalPointer()); qDebug() << model->getData("ItemType"); return QVariantMap();
}
The result is
QVariant(QString, "Project") QVariant(QString, "Project")
This function got call twice, that's true ( because i have 2 items in treeview, one is the root, and other is the item i added) but result i wrong.
And if i add one more item under root, this will crash.@illunara Sorry but I'm unable to figure out what you are trying to do from the code.
What do you expect it to print ? "Hello World" ?
Why are you casting index it to model ?index.model()
should be a better way to do.
If you meant to cast it to anProject
object and get its data it should be something likeProject *item = static_cast<Project*>(index.internalPointer()); return item->data(0).toString();
Perhaps you should elaborate it more precisely.
-
@illunara Sorry but I'm unable to figure out what you are trying to do from the code.
What do you expect it to print ? "Hello World" ?
Why are you casting index it to model ?index.model()
should be a better way to do.
If you meant to cast it to anProject
object and get its data it should be something likeProject *item = static_cast<Project*>(index.internalPointer()); return item->data(0).toString();
Perhaps you should elaborate it more precisely.
@p3c0 Yup i think the output should be
QVariant(QString, "Project") QVariant(QString, "Hello World")
Oh and, Project is delivered class of ModelAbstract. Sorry, my fault
class ModelAbstract { public: ModelAbstract(); virtual QVariant getData(QString dataname) const = 0; } class Project : public ModelAbstract { public: virtual Variant getData(QString dataname) const; }
-
@p3c0 Yup i think the output should be
QVariant(QString, "Project") QVariant(QString, "Hello World")
Oh and, Project is delivered class of ModelAbstract. Sorry, my fault
class ModelAbstract { public: ModelAbstract(); virtual QVariant getData(QString dataname) const = 0; } class Project : public ModelAbstract { public: virtual Variant getData(QString dataname) const; }
-
@illunara So you mean
getData
should return aQVariantMap
? Is that the problem ? Can you post the definition ofgetData
method ?@p3c0 Sorry for confuse you, but just focus on this
ModelAbstract* model = static_cast<ModelAbstract*>(index.internalPointer()); qDebug() << model->getData("ItemType");
What i meant here is, the index of styleData.value given in tableviewColumn is wrong. Since the output value is same one item.
And here is the definition of getData(QString dataname)virtual QVariant getData(QString dataname) const; QVariant Project::getData(QString dataname) const { if(dataname == "ItemDisplayName") { QVariantMap returnData; returnData["ItemDisplayName"] = m_name; returnData["ItemType"] = m_type; return returnData; } else if(dataname == "ItemType") return "Project"; else return QVariant(); }
I also have a Q_INVOKABLE getData(QString dataname) inside QAbstractItemModel class,
-
@p3c0 Sorry for confuse you, but just focus on this
ModelAbstract* model = static_cast<ModelAbstract*>(index.internalPointer()); qDebug() << model->getData("ItemType");
What i meant here is, the index of styleData.value given in tableviewColumn is wrong. Since the output value is same one item.
And here is the definition of getData(QString dataname)virtual QVariant getData(QString dataname) const; QVariant Project::getData(QString dataname) const { if(dataname == "ItemDisplayName") { QVariantMap returnData; returnData["ItemDisplayName"] = m_name; returnData["ItemType"] = m_type; return returnData; } else if(dataname == "ItemType") return "Project"; else return QVariant(); }
I also have a Q_INVOKABLE getData(QString dataname) inside QAbstractItemModel class,
@illunara According to your earlier code:
QVariantMap TreeViewDataModel::get(QModelIndex index) { ModelAbstract* model = static_cast<ModelAbstract*>(index.internalPointer()); qDebug() << model->getData("ItemType"); return QVariantMap();
You are fetching data for "ItemType". And now the definition of
getData
:virtual QVariant getData(QString dataname) const; QVariant Project::getData(QString dataname) const { if(dataname == "ItemDisplayName") { QVariantMap returnData; returnData["ItemDisplayName"] = m_name; returnData["ItemType"] = m_type; return returnData; } else if(dataname == "ItemType") return "Project"; else return QVariant(); }
Isn't it obvious that you will get "Project" always for "ItemType" ?
-
@illunara According to your earlier code:
QVariantMap TreeViewDataModel::get(QModelIndex index) { ModelAbstract* model = static_cast<ModelAbstract*>(index.internalPointer()); qDebug() << model->getData("ItemType"); return QVariantMap();
You are fetching data for "ItemType". And now the definition of
getData
:virtual QVariant getData(QString dataname) const; QVariant Project::getData(QString dataname) const { if(dataname == "ItemDisplayName") { QVariantMap returnData; returnData["ItemDisplayName"] = m_name; returnData["ItemType"] = m_type; return returnData; } else if(dataname == "ItemType") return "Project"; else return QVariant(); }
Isn't it obvious that you will get "Project" always for "ItemType" ?
-
@p3c0 You are right, my horrible mistake. I think i drunk, should get home now :(
I marked this topic as solved, should i some delete needless post too?