On this lovely weekend I’m digging through Qt example tutorials and try to translate the examples to python (using PySide2).
I just finished this example: https://doc.qt.io/qt-5/qtwidgets-itemviews-coloreditorfactory-example.html
But PySide2 needed some special treatment because it is missing QVariant as well as QMetaType.
The PySide2 documentation says that where ever one has to use a QVariant, native python types can be used instead.
But in case of QItemEditorFactory().registerEditor this is not true, since its expecting an integer as its first parameter.
Looking at PyQt5 the type id for QColor can be easily obtained using QVariant.Color or QMetaType.QColor
in PySide2 I need to either write down 67 (since that is the userType id for QColor) or get the id using staticMetaObject from the class where I have a user property defined:
Given this class
class ColorListEditor(QComboBox): def __init__(self, *args, **kwargs): super(ColorListEditor, self).__init__(*args, **kwargs) self.populateList() def getColor(self): return self.itemData(self.currentIndex(), Qt.DecorationRole) def setColor(self, color): self.setCurrentIndex(self.findData(color, Qt.DecorationRole)) color = Property(QColor, getColor, setColor, user=True) def populateList(self): color_names = QColor.colorNames() for index, name in enumerate(color_names): color = QColor(name) self.insertItem(index, name) self.setItemData(index, color, Qt.DecorationRole)
this is how I would obtain the QColor userType id then:
Both approaches feel kind of ‘meh’ to me and I thought maybe you guys 'n girls know a smarter way for PySide2 to get the userType.
How to construct a custom userType for custom classes in PySide2?