thedurrrr
thedurrrr

Reputation: 3

QML/Python Combobox print selected item issue

I'm using PySide2 and creating a combobox, I'm trying to send the selected value when pressing the submit button, visual example:

enter image description here

when pressing submit test1 should be print on teminal, I tried using Slots but when pressing on the submit button nothing happens.

my code: main.py:

import sys
import os
from os.path import join, dirname, abspath
from PySide2.QtCore import QStringListModel, QObject, Slot
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine

class Combo(QObject):
    def __init__(self, parent=None):
        super().__init__()
        self.model = QStringListModel(['test1', 'test2'])
    
    @Slot(str, result=None)
    def submit(self, text_val):
        print('test')
        print(text_val, self.model.text)



if __name__ == '__main__':
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    comobo_box = Combo()

    context = engine.rootContext()
    context.setContextProperty("comobo_box", comobo_box)
    context.setContextProperty("stringModel", comobo_box.model)
    qmlFile = join(dirname(__file__), r'main.qml')
    engine.load(abspath(qmlFile))


    if not engine.rootObjects():
        sys.exit(-1)

    app.exec_()

main.qml:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15


Window {
    id: window
    visible: true
    height: 200
    width: 400

    property string textVal: ""

    Rectangle {
        color: '#041645'
        id: mainArea
        anchors.fill: parent
        ColumnLayout {
            anchors.fill: parent

            ComboBox {
                id: cBox
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                model: stringModel
                textRole: "display"


                    Button {
                    id: submit
                    x: -50
                    y: 100
                    text: qsTr("Submit")
                    onClicked: {
                        comobo_box.submit(model.text)

                       }
                }
                    Button {
                    id: cancel
                    x: 75
                    y: 100
                    text: qsTr("Cancel")
                    onClicked: {
                        window.close()

                                    }
                }


            }
            RowLayout{
                    Layout.alignment: Qt.AlignHCenter

            }

        }

    }
}

Upvotes: 0

Views: 264

Answers (1)

eyllanesc
eyllanesc

Reputation: 244152

The problem is that there is no model object, also assuming that you refer to the ComboBox model, it also does not have the text attribute and that is indicated in the log.

file:///home/qt/main.qml:35: ReferenceError: model is not defined

In this case it is passing the currentText:

comobo_box.submit(cBox.currentText)

On the python side there is a similar problem so you have to remove self.model.text:

@Slot(str)
def submit(self, text_val):
    print(text_val)

Upvotes: 1

Related Questions