When I was working on the QAbstractTableModel Tutorial and had just added the block that formats strings into things like dates, floats, and integers, I accidentally omitted the first line of the data method block, if role == Qt.DisplayRole:
.
The code still ran, but the output looked rather strange and I was wondering why this happened.
This was the code that I had just added, but accidentally omitted the first line:
def data(self, index, role):
if role == Qt.DisplayRole: #omitting this accidentally caused the error
# Get the raw value
value = self._data[index.row()][index.column()]
# Perform per-type checks and render accordingly
if isinstance(value, datetime.date):
# Render time to YYY-MM-DD.
return value.strftime("%Y-%m-%d")
if isinstance(value, float):
# Render float to 2 dp
return "%.2f" % value
if isinstance(value, str):
# Render strings with quotes
return '"%s"' % value
# Default (anything not captured above: e.g. int)
return value
This is what the output looked like:
I am very curious as to why this happened - there are check boxes, check marks, and a square filled in.
Thank you for your insights!