python - QML ComboBox 中的 Python 列表
问题描述
QML 新手。试图将 python 列表设置为 QML ComboBox。我创建了一个小的使用示例。请注意,在实际程序中,python 列表不是硬编码的。
蟒蛇类:
class Test(QObject):
def __init__(self,):
super().__init__()
@Slot(result=list)
def getList(self):
return ["one", "two", "three"]
主要.py:
def main():
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
test = Test()
engine.rootContext().setContextProperty("Test", test)
engine.load(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__), 'main.qml')))
if not engine.rootObjects():
raise Exception("No qml objects were loaded!")
return app.exec_()
if __name__ == '__main__':
sys.exit(main())
QML 组件:
ComboBox {
id: combo
width: parent.width
model: Test.getList() // no idea what to write here
textRole: "fileName"
onAccepted: {
print(combo.currentText)
}
}
谢谢!
解决方案
推荐阅读
- docker - 未找到 kubernetes microk8s 节点端口
- react-native - 表单无效时如何让Formik调用函数
- python - Python - 使用 SQLAlchemy 创建嵌套连接查询
- c# - 将程序集版本写入字符串并将其保存到文本文件 C#
- regex - 正则表达式捕获具有相同名称的文件名和文件夹
- kubernetes - minikube 上的简单 kubernetes 部署,helm 3 不工作(无法访问应用程序)
- python - 组合类及其组件中的同时多重继承(Python)
- python - Python:在列表列表中交换值?
- ios - 为某些项目安装 pod 时,如何将情节提要添加到我的框架并作为资源出现在库 pod 中
- java - 使用 thymleaf 更改会话属性值