Reputation: 2456
I have a class overriding the text() method of QtGui.QStandardItem:
class SourceFileItem(QtGui.QStandardItem):
def __init__(self, name):
super(SourceFileItem, self).__init__("Original Name")
def text(self):
return "Name from Override"
But when I add this to a QStandardItemModel and set that as the model for a listView only the text from the call to the parent class' init method is displayed and there's no evidence of my overridden method being called at all.
The documentation seems to indicate this is the right method for returning the display text:
PySide.QtGui.QStandardItem.text()
Return type: unicode
Returns the item’s text. This is the text that’s presented to the user in a view.
Upvotes: 1
Views: 476
Reputation: 120608
This won't work, because QStandardItem.text
isn't a virtual function.
As you've already discovered, overriding a non-virtual function is mostly useless, because although it will work fine on the Python side, it won't be visible on the C++ side, and so it will never be called internally by Qt.
However, in this particular case, all is not lost, because the text()
function is roughly equivalent to this:
def text(self):
return self.data(QtCore.Qt.DisplayRole)
and QStandardItem.data
is virtual.
So all you need is something like this:
def data(self, role=QtCore.Qt.UserRole + 1):
if role == QtCore.Qt.DisplayRole:
return 'Name from Override'
return super(SourceFileItem, self).data(role)
and now your overidden data()
function will be called by Qt from within its text()
function.
Upvotes: 1