Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML ComboBox - how to validate selected item?



  • Hello, I'm a beginner in Qt.
    I'm having trouble with ComboBoxes, I have a form, user fills the form, I save data in my db, but if the user wants to edit/update them, I have trouble with comboboxes, I can load data to combobox, but I also want to show the item user previously selected and validate it before updating (as i have -insert- form, i have also -update- form)

    this is my combobox:

    RowLayout {
                    Layout.fillWidth: true
                    ComboBox {
                        id: comboPerson
                        Layout.minimumWidth: 170
                        editable: true
                        textRole: "personName"
                        model: ListModel {
                            id: comboPersonListModel
                            Component.onCompleted: JS.loadComboPerson()
                        }
                        delegate: ItemDelegate {
                            width: comboPerson.width
                            height: comboPerson.height
                            font.pixelSize: 15
    
                            contentItem: Label {
                                width: comboPerson.width
                                height: comboPerson.height
                                text: personName
                                font.pixelSize: 15
                                topPadding: 12
                            }
                        }
                    }
                }
    

    how i load data:

    function loadComboPerson() {
        var db = open()
        db.transaction(function (tx) {
            var results = tx.executeSql(
                        'SELECT personId, personName FROM persons WHERE personId IS NOT 0')
            for (var i = 0; i < results.rows.length; i++) {
                comboPersonListModel.append({
                                                personName: results.rows.item(i).personName,
                                                personId: results.rows.item(i).personId
                                            })
            }
        })
    }
    

    but i also want to show the item user previously selected:

    function selectProject(idProject) {
    var db = open()
    db.transaction(function (tx) {
        var results = tx.executeSql(
                    'SELECT projectName, clientName, contact, realizationAdd, address, colorRectangle, realizationTypes.realizationType, todayDate, testDate, launchDate,special, projects.notes, persons.personName FROM projects INNER JOIN realizationTypes ON projects.realizationTypeId = realizationTypes.realizationTypeId INNER JOIN persons ON projects.personId = persons.personId WHERE projects.projectId = ?', [idProject])
        if (results.rows.length > 0) {
            var result = results.rows.item(0)
    
            projectNameId.text = result.projectName;
            clientId.text = result.clientName;
            contactId.text = result.contact;
            addressId.text = result.address;
            realizationId.text = result.realizationAdd;
            comboRealization.editText = result.realizationType;
            todayDateId.text = result.todayDate;
            testDateId.text = result.testDate;
            launchDateId.text = result.launchDate;
            specialId.text = result.special;
            notesId.text = result.notes;
            comboPerson.editText = result.personName; // here is the part from example
            colors.editText = result.colorRectangle;
        } else {
            console.log("n");
        }
    })
    

    I'm not sure if I should use editText, I just want the item that was selected by the user to be in my combobox as selected item BUT when user wont change this combobox, currentIndex is -1 so its empty and i cant process my update form (where I have if statement if currentIndex is >=0)

    I tried: currentText - its read only, displayText - it is just the text that its displayed on combobox button (if i understand right)



  • @artangel Hey, if the combobox list is not changed anytime in the application, you can just store the selected currentIndex and when the user want to edit you can just set the right index with setCurrentIndex(index)


Log in to reply