Making Only One Column of a Qtreewidgetitem Editable

Making only one column of a QTreeWidgetItem editable

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.

Making only one column of QTreeWidget editable // troubleshooting

As mentioned in the documentation:

The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.

It means that it won't work for all use cases. The solution is to create your own model and overload the flags(const QModelIndex& index) method returning the appropriate values (basically Qt:: ItemIsEnabled for read-only columns and Qt:: ItemIsEnabled | Qt::ItemIsEditable for the editable one). You can get the column from index.column().

Qt provides an example to start with trees and models.

How to edit only one column of a QTreeWidgetItem

There are no methods / flags to configure this directly.

You can solve this by switching off Qt's EditTriggers completely and implementing your own function that decides whether an item should be edited or not:

class MyWidget(QWidget):

def __init__(parent):

self.treeWidget = ...

# switch off "default" editing behaviour
# as it does not allow to configure only an individual
# column as editable
self.treeWidget.setEditTriggers(self._treeWidget.NoEditTriggers)

# to be able to decide on your own whether a particular item
# can be edited, connect e.g. to itemDoubleClicked
self.treeWidget.itemDoubleClicked.connect(self.checkEdit)

# in your connected slot, you can implement any edit-or-not-logic
# you want
def checkEdit(self, item, column):
# e.g. to allow editing only of column 1:
if column == 1:
self.treeWidget.editItem(item, column)

If you'd like to editing on other occasions as well, simply connect checkEdit to the according signals, such as itemClicked, itemEntered etc.

QTreeWidgetItem editable allow entering number only

I think there are a number of methods, but overloading of QItemDelegate::createEditor is a more useful than others.

You should write code with QValidator, in your case, use QIntValidator.

class CustomDelegate : public QItemDelegate
{
public:
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
QLineEdit *lineEdit = new QLineEdit(parent);

// Set your validator, such as 'only number between 0 ~ 9 )
QIntValidator *validator = new QIntValidator(0, 9, lineEdit);
lineEdit->setValidator(validator);

return lineEdit;
}
};

And then, set your custom delegate to your tree.

m_pPropertyTree->setItemDelegate( new CustomDelegate );

How to make QTreeWIdgetItems editable selectively

Generally, you would do this with the use of a QItemDelegate/QStyledItemDelegate. Delegates give you control over how data from the model is displayed in the view and how data from the controller/view is edited and inserted back into the model. They allow you to do things like custom painting and custom editors. In this case, we just check the column index and refuse to return an editor for anything but the first column.

class MyDelegate(QtGui.QItemDelegate):

def createEditor(self, parent, option, index):
if index.column() == 0:
return super(MyDelegate, self).createEditor(parent, option, index)
return None

delegate = MyDelegate()
tree.setItemDelegate(delegate)

How to edit QTreeWidgetItem when it is editable

Sample Image

Subclass QTreeWidgetItem. Define setData method to assign the value to the attribute _name.

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class TreeWidgetItem(QtGui.QTreeWidgetItem):
def __init__(self, parent=None):
super(TreeWidgetItem, self).__init__(parent)

def setData(self, column, role, value):
super(TreeWidgetItem, self).setData(column, role, value)
self._name = value.toString()

class Tree(QtGui.QTreeWidget):
def __init__(self, *args, **kwargs):
super(Tree, self).__init__()
for i, item_name in enumerate(['Item_1','Item_2','Item_3','Item_4','Item_5']):
rootItem = TreeWidgetItem(self)
rootItem.setFlags(rootItem.flags() | QtCore.Qt.ItemIsEditable)
rootItem._name = 'Root %s'%i
rootItem.setText(0, rootItem._name)

for number in range(3):
childItem = TreeWidgetItem(rootItem)
childItem.setFlags(rootItem.flags() | QtCore.Qt.ItemIsEditable)
childItem._name = 'Child %s'%number
childItem.setText(0, childItem._name)

self.addTopLevelItem(rootItem)

self.clicked.connect(self.onClick)
self.show()

def onClick(self, index):
print self.currentItem()._name


Related Topics



Leave a reply



Submit