【发布时间】:2021-06-09 10:15:55
【问题描述】:
我正在使用 pyqt5 并且想要获取将出现在组合框下拉菜单上的元素列表,我正在尝试实例化 ComboBox 的“模型”属性,但似乎没有任何反应,而且该方法似乎没有去工作。 我应该使用与@pyqtSlot 不同的装饰器吗?或者这是另一回事?
我的代码:
qml:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Dialogs 1.2
import "../components"
Window {
id: window
visible: true
height: 200
width: 400
title: "test"
ManualSelector { id: customText }
Rectangle {
color: '#041645'
id: mainArea
anchors.fill: parent
ColumnLayout {
anchors.fill: parent
ComboBox {
id: cBox
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
model: instance.getList()
textRole: "display"
}
RowLayout{
Layout.alignment: Qt.AlignHCenter
PressButton {
id: pressButtonTwo
width: 150
height: 50
text: "Quit"
onClicked: window.close()
}
PressButton {
id: pressButtonOne
width: 150
height: 50
text: "Submit"
onClicked:{
window.close()
}
}
}
}
}
}
python 代码:
python:
self.set_qml_context_property("instance", self.instance())
@pyqtSlot(name="getList")
def get_names(self):
names_for_dropdown = ['1', '2', '3']
return names_for_dropdown
【问题讨论】: