I have a QTreeWidgetItem
with two columns of data, is there any way to make only the second column editable? When I do the following:
QTreeWidge
Set the child of the tree-widget editable or not(itmes of tree), based on the row and column.
class EditorDelegate : public QItemDelegate
{
Q_OBJECT
public:
EditorDelegate(QObject *parent):QItemDelegate(parent){};
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
QWidget* EditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column() == 1)
{
return QItemDelegate::createEditor(parent, option, index);
}
return nullptr;
}
In the QTreeWidget
:
myQTreeWidget::myQTreeWidget()
{
EditorDelegate *d = new EditorDelegate(this);
this->setItemDelegate(d);
}
Maybe a little late, but may help :
void MyClass::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column) {
Qt::ItemFlags flags = item->flags();
if(column == 0)
{
item->setFlags(flags & (~Qt::ItemIsEditable));
}
else
{
item->setFlags(flags | Qt::ItemIsEditable);
}
}
Here 0 is the index of the column you want to make readonly.
flags & (~Qt::ItemIsEditable)
Sets the ItemIsEditable position to 0 regardless the previous flag of your item.
flags | Qt::ItemIsEditable
Sets it to 1 regardless the previous flag.
I had the same problem recently and discovered a solution which works with all EditTriggers, not only the DoubleClicked one (and the connection to the double clicked signal)
Create a Delegate, that returns a NULL Pointer for the editor:
class NoEditDelegate: public QStyledItemDelegate {
public:
NoEditDelegate(QObject* parent=0): QStyledItemDelegate(parent) {}
virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
return 0;
}
};
And later use it as a custom delegate for your column
ui->parameterView->setItemDelegateForColumn(0, new NoEditDelegate(this));
Seem like the standard QTreeWidget doesn't allow this. I think there are two ways to do this:
Use a QTreeView with your own class derived from QAbstractItemModel and override the flags function
Use a QTreeView with a QStandardItemModel. Then when you add the item just set the appropriate column to allow edits:
Here's some code for the second option:
QString x, y;
QList<QStandardItem*> newIt;
QStandardItem * item = new QStandardItem(x);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
newIt.append(item);
item = new QStandardItem(y);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);
newIt.append(item);
model->appendRow(newIt);
I find the second approach simpler but that depends on how much flexibility you want with your model.
Looks like you will have to forgo using QTreeWidget
and QTreeWidgetItem
and go with QTreeView
and QAbstractItemModel
. The "Widget" classes are convenience classes that are concrete implementations of the more abstract but more flexible versions. QAbstractItemModel
has a call flags(QModelIndex index)
where you would return the appropriate value for your column.