Search code examples
qtpyqtpysidetext-stylingqstandarditem

How to strikeout text of QStandardItem in Qt/PyQt?


I have a QStandardItemModel in PySide, and want to strikeout text on certain rows (it is a to do list application and when a task is done, I want to strike through the text). Given a QStandardItem that displays fine, based on the documentation I try to strikethrough the text with:

QStandardItem.setFont(QtGui.QFont.setStrikeOut(True))

But the text is unchanged, and I get error:

TypeError: descriptor 'setStrikeOut' requires a 'PySide.QtGui.QFont' object but received a 'bool'

Solution

  • setStrikeOut returns void and you should not pass it as the argument for setFont. You should pass an instance of QFont there.

    It would be better to retrieve the font of the item in question, set its strikeout property, and then set this revised QFont as the new font of the item :

    f = item.font()
    f.setStrikeOut(True)
    item.setFont(f)
    

    This way you would keep other options of the item's font previously set.