pythonguis / feedback

Corrections & suggestions for Python GUIs tutorials on pythonguis.com
1 stars 0 forks source link

Typing in: Displaying tabular data in Qt6 ModelViews #12

Open Protomolekule opened 6 months ago

Protomolekule commented 6 months ago

Thanks for the helpful manual. But there is a little error.


def data(self, index, role):
    # existing if role == **Qt.ItemDataRole.DisplayRole:** block hidden
    # hidden for clarity.

    if role == **Qt.BackgroundRole** and index.column() == 2:
        # See below for the data structure.
        return QtGui.QColor('blue')

should be Qt.ItemDataRole.BackgroundRole

Further:

If I use the following snipped, no Icon will be shown. I've used your data example:

   def data(self, index, role):
        if role == Qt.ItemDataRole.DisplayRole:
            value = self._data[index.row ()][index.column ()]
            if isinstance (value, datetime):
                return value.strftime ('%Y-%m-%d')

            return value

        if role == Qt.ItemDataRole.DecorationRole:
            value = self._data[index.row ()][index.column ()]
            if isinstance (value, datetime):
                return QtGui.QIcon ('calendar.png')
        if role == Qt.ItemDataRole.DecorationRole:
            value = self._data[index.row ()][index.column ()]
            if isinstance (value, bool):
                if value:
                    return QtGui.QIcon ('tick.png')

                return QtGui.QIcon ('cross.png')

BTW: Could you extend this example for Qt.ItemDataRole.CheckStateRole. I can't get a checkbox with returning the changed value and reflecting the state in the TableView