Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML ComboBox - how to validate selected item?
Qt 6.11 is out! See what's new in the release blog

QML ComboBox - how to validate selected item?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 565 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    artangel
    wrote on last edited by
    #1

    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)

    M 1 Reply Last reply
    0
    • A artangel

      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)

      M Offline
      M Offline
      MEMekaniske
      wrote on last edited by
      #2

      @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)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved