【问题标题】:QML/Python Getting a list of elements for ComboBox dropdown menuQML/Python 获取 ComboBox 下拉菜单的元素列表
【发布时间】: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

【问题讨论】:

    标签: python pyqt5 qml


    【解决方案1】:

    通过更改为以下内容设法解决了问题:

    @pyqtSlot(result=QVariant, name="getList")
    

    和:return QVariant(names_for_dropdown) 也删除 textRole: "display" 来自组合框

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多