Fab*_*ian 5 python qt qml qt-quick
我正在编写一个小程序,它使用Qt5 QML作为GUI层,使用Python3-PyQt5来实现数据模型.
我现在想要ComboBox在QML中显示并将其模型设置为枚举列表.我如何将枚举作为python类的属性导出,以便我可以在QML中引用它?
我最好用QML写这个:
ComboBox {
model: mymodel.car_manufacturers
onCurrentIndexChanged: mymodel.selected_manufacturer = currentIndex
}
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案,对我来说足够好。在 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()
Run Code Online (Sandbox Code Playgroud)
在 QML 中我有:
ComboBox {
model: myModel.carmanufacturers
currentIndex: myModel.mfc
onCurrentIndexChanged: myModel.mfc = currentIndex
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
966 次 |
| 最近记录: |