pySide2组合框与pyqt5组合有很大不同吗?
这是我的qml,在同一个foder中我有两个python文件,一个有pyqt5绑定,一个有pySide,大多数其他东西我没有问题绑定,但我在使用组合框时出错了。
import QtQuick 2.10
import QtQuick.Controls 2.1
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.3
ApplicationWindow {
id: applicationWindow
Material.theme: Material.Light
title: qsTr("Test Invoke")
visible: true
width: 600
height: 500
ComboBox {
id: comboBox
width: 200
model: Manager.stations
}
}
pyqt5只需要这个
import sys
import os
from PyQt5.QtCore import QObject, QUrl, Qt, pyqtSlot, pyqtSignal, pyqtProperty
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5 import QtGui
my_list = ['here','is','my','list']
class Manager(QObject):
stationsChanged = pyqtSignal()
def __init__(self):
QObject.__init__(self)
self.m_stations = []
# self.m_currentStation = ""
#self.m_currentValue =0
@pyqtProperty(list, notify=stationsChanged)
def stations(self):
return self.m_stations
@stations.setter
def stations(self, val):
if self.m_stations == val:
return
self.m_stations = val[:]
self.stationsChanged.emit()
def list_fill(self):
self.stations = my_list
if __name__ == "__main__":
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
manager = Manager()
ctx = engine.rootContext()
ctx.setContextProperty("Manager", manager)
engine.load('main.qml')
if not engine.rootObjects():
sys.exit(-1)
app.setWindowIcon(QtGui.QIcon('ico.png'))
manager.list_fill()
sys.exit(app.exec_())
和pySide2是:
import sys
import os
from PySide2.QtCore import Qt, QObject, Signal, Slot, Property
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
my_list = ['here','is','my','list']
class Manager(QObject):
currentValueChanged = Signal()
stationsChanged = Signal()
def __init__(self):
QObject.__init__(self)
self.m_stations =[]
def get_stationsChanged(self):
return self.m_stations
def set_stationsChanged(self, val):
if self.m_stations == val:
return
self.m_stations = val[:]
self.stationsChanged.emit()
def list_fill(self):
self.stations = my_list
currentValue = Property(list, get_stationsChanged, set_stationsChanged, notify=stationsChanged)
if __name__ == "__main__":
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
manager = Manager()
ctx = engine.rootContext()
ctx.setContextProperty("Manager", manager)
engine.load('main.qml')
if not engine.rootObjects():
sys.exit(-1)
manager.list_fill()
sys.exit(app.exec_())
我很确定我在pySide2上分配属性时会出错,但我不知道如何纠正它,我没有得到任何错误或任何错误,所以我很难找到答案,或者至少提示可能有问题。
PyQt5工作正常,但是pyside2没有填充组合框,或提供有关它为什么不这样做的任何信息。