我正在编写一个小程序,它使用 Qt5 QML 作为 GUI 层,并使用 Python3-PyQt5 来实现数据模型。
我现在想显示一个ComboBox
在 QML 中并将其模型设置为枚举列表。如何将枚举导出为 python 类的属性,以便我可以在 QML 中引用它?
我最好用 QML 写这个:
ComboBox {
model: mymodel.car_manufacturers
onCurrentIndexChanged: mymodel.selected_manufacturer = currentIndex
}
这是我的解决方案,对我来说足够好。
在 python 代码中我有以下内容:
class CarManufacturers(enum.Enum):
BMW, Mercedes = range(2)
mfcChanged = pyqtSignal()
@pyqtProperty('QStringList', constant=True)
def carmanufacturers(self):
return [mfc.name for mfc in CarManufacturers]
@pyqtProperty('QString', notify=mfcChanged)
def mfc(self):
return str(CarManufacturers[self._mfc].value)
@modus.setter
def mfc(self, mfc):
print('mfc changed to %s' % mfc)
if self._mfc != CarManufacturers(int(mfc)).name:
self._mfc = CarManufacturers(int(mfc)).name
self.mfcChanged.emit()
在 QML 中我有:
ComboBox {
model: myModel.carmanufacturers
currentIndex: myModel.mfc
onCurrentIndexChanged: myModel.mfc = currentIndex
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)