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. Using Combobox in delegate does to trigger model to change data

Using Combobox in delegate does to trigger model to change data

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.0k Views
  • 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.
  • C Offline
    C Offline
    Celal
    wrote on last edited by
    #1

    Hello,

    Recently I decided to try QML, but I have some problems. QML is soo cool but it looks like to me at the same time it is tricky too :)

    Well, let's look at my problem. I have a ListModel that in c++ and set to TableView in QML. The model consists of 4 roles. When I use TextField in delegate everything is working as expected.

    But What I want is Add Combobox for delegate to make selection and change data. Whenever I start use combo box I am no longer to change data in the model.

    Working code with TextField:

      TableViewColumn {
                    role: "name"
                    title: "Name"
                    width: 250
                    delegate: TextField {
                        anchors.verticalCenter: parent.verticalCenter
                        text: model.name
                        onEditingFinished: model.name = text
                    }
                }
    

    Problematic code:

    TableViewColumn {
                    role: "name"
                    title: "Name"
                    width: 120
                    delegate: ComboBox {
                        anchors.verticalCenter: parent.verticalCenter
                        model: ["Alice", "Bob"]
                        currentIndex: model.name === "Alice" ? 0 : 1
                        onCurrentTextChanged: {
                            console.log("from component", currentText)
                            model.name = currentText
                        }
                    }
                }
    

    What is the reason causing this problem?

    Thanks in advance.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      ComboBox itself has model. When you access the model, it is changing the model of combobox not the parent model.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Celal
        wrote on last edited by
        #3

        @dheerendra thank you for your response. So what is the solution then? I cannot use combobox in MVC?

        What is the alternative way to solve this problem?

        Thanks

        E 1 Reply Last reply
        0
        • C Celal

          @dheerendra thank you for your response. So what is the solution then? I cannot use combobox in MVC?

          What is the alternative way to solve this problem?

          Thanks

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @Celal Try this idea (no full code here):

          delegate: Item {
              id: i
              ComboBox {
                  oncurrentTextChanged: {
                      i.model.name = currentText
                  }
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Celal
            wrote on last edited by
            #5

            @Eeli-K It does not work in your way. It was giving "Type error"

            However, I like your approach. So then I tried something else.

            Instead of id, I define a property for each row then I use property to assign the new value then it works

            here is the solution:

              delegate: RowLayout {
                            property var rmodel: model
                            ComboBox {
                                model: ListModel {
                                    ListElement {name : "9600"}
                                    ListElement {name : "112500"}
                                }
                                textRole: "name"
                                currentIndex: rmodel.baudrate === "9600" ? 0 : 1
                                onCurrentTextChanged: {
                                    rmodel.baudrate = currentText
                                }
                            }
            
            

            Thanks for help.

            Celal

            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