How to get Persistant delegate for single column for TreeView using AbstractItemModel
Unsolved
General and Desktop
-
Hi ,
I have created a Treeview using QAbstractItemModel.
My Parent item has only one column and children have 3.
My second column for is editable and uses and line edit as a deligate.
I need a persistent tool button as a delegate for the third column.
The possible methods are setIndexWidget and Openpersistanteditor.
In either ways i dont know how to get the index of the item.
i am able to set seperate delegates using setItemDelegateForColumn
Kindly helpThese are my classes
class TreeItem { public: TreeItem(QString a ,QString b); TreeItem(const QString &alias_name); bool removeChildren(int position, int count); ~TreeItem(); QString A; QString B; TreeItem *parent; QList<TreeItem*> children; };
class TreeModel : public QAbstractItemModel { public: TreeModel (QTreeView *TreeItem,QObject *parent = 0); ~TreeModel (); void setRootNode(TreeItem*node); QModelIndex index(int row, int column, const QModelIndex &parent) const; QModelIndex parent(const QModelIndex &child) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; void addChild(TreeItem*parent, TreeItem*child); bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); TreeItem * getItem(const QModelIndex &index) const; TreeItem *nodeFromIndex(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; int rowno; private: TreeItem*rootNode; QTreeView *parametertree; QModelIndex *parentindex; }; TreeModel::TreeModel(QTreeView *treeview,QObject *parent): QAbstractItemModel(parent) { rootNode = 0; parametertree=treeview; rowno=0; } void TreeModel::setRootNode(TreeItem *node) { // because reset() was deprecated on Qt 5.0 beginResetModel(); delete rootNode; rootNode = node; rootNode->parent=NULL; endResetModel(); } QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const { if (!rootNode || row < 0 || column < 0) return QModelIndex(); TreeItem *parentNode = nodeFromIndex(parent); TreeItem *childNode = parentNode->children.value(row); if (!childNode) return QModelIndex(); QModelIndex childindex=createIndex(row, column, childNode); if(childindex.column()==2) { if(childindex.row()==2) qDebug()<<"row:"<<childindex.row()<<" col:"<<childindex.column(); // parametertree->openPersistentEditor(childindex); } return childindex; } QModelIndex TreeModel::parent(const QModelIndex &child) const { TreeItem*node = nodeFromIndex(child); if (!node) { return QModelIndex(); } TreeItem*parentNode = node->parent; if (!parentNode) return QModelIndex(); TreeItem*grandparentNode = parentNode->parent; if (!grandparentNode) return QModelIndex(); int row = grandparentNode->children.indexOf(parentNode); return createIndex(row, 0, parentNode); } int TreeModel::rowCount(const QModelIndex &parent) const { TreeItem*parentItem = getItem(parent); if (!parentItem) return 0; return parentItem->children.count(); } int TreeModel::columnCount(const QModelIndex & /* parent */) const { return 3; } QVariant TreeModel::data(const QModelIndex &index, int role) const { ...... } QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { if (section == 0) { return tr("Item"); } else if (section == 1) { return tr("Value"); } else if (section == 2) { return tr("Download"); } } return QVariant(); } void TreeModel::addChild(TreeItem *parent,TreeItem *child) { if (child) { parent->children += child; child->parent = parent; ; } } bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) { TreeItem *parentItem = rootNode; bool success = true; beginRemoveRows(parent, position, position + rows - 1); success = parentItem->removeChildren(position, rows); endRemoveRows(); resetInternalData(); return success; } TreeItem *TreeModel::nodeFromIndex(const QModelIndex &index) const { if (index.isValid()) { return static_cast<TreeItem *>(index.internalPointer()); } else { return rootNode; } } Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const { int col = index.column(); if (col==1) { return (QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled); } else if (col==2) { return (QAbstractItemModel::flags(index)| Qt::ItemIsEnabled | Qt::ItemIsEditable ); //result |= Qt::Itemi; } return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled ; } bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) { .... } TreeItem *TreeModel::getItem(const QModelIndex &index) const { if (index.isValid()) { TreerItem *item = static_cast<TreeItem*>(index.internalPointer()); if (item) return item; } return rootNode; }