[Solved] Invidual itemDelegate for each column of table\listview
-
@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?