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. Updating C++ Model Data from QML Listview
Forum Updated to NodeBB v4.3 + New Features

Updating C++ Model Data from QML Listview

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 2.6k 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.
  • DonCoderD Offline
    DonCoderD Offline
    DonCoder
    wrote on last edited by
    #1

    I have a textfield and checkbox in listview , Now i want to update model data when checkbox state changed. also textfiled should be editable when clicked so i should be able to update data in the C++ model
    CheckBox {
    anchors.right: parent.right
    checked: cFav
    onCheckedChanged:
    {
    // Model name is contactModel

        }
    }
    

    Please help me with this.

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

      Assuming that contact model is exposed from c++ side. Expose some method as slot in your model, call that method and update your model data structure in backend

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

      1 Reply Last reply
      6
      • DonCoderD Offline
        DonCoderD Offline
        DonCoder
        wrote on last edited by
        #3

        Thanks Dheerendra. Yes i have exposed the model as a context property. I agree with you that i can call a slot and pass index and value to update model. But my question is is there any way like, onCheckedChanged i can assign value to contactModel. I mean to say contactModel.setproperty(index, role, value). May be i am not correct my whole intention behind this is to make Models setData should be called directly from QML Listview

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

          No direct way like this. Either you expose some property through q_property and assign the value. Internally in you model it will call set method. Or else you need go through exposed slot

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

          1 Reply Last reply
          8
          • DonCoderD Offline
            DonCoderD Offline
            DonCoder
            wrote on last edited by
            #5

            Thanks for the clarification.

            1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              If you want your listview to be generic, you can call QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) from QML since it is a Q_INVOKABLE, the hard part is figuring out the role number.
              If your listview is tailor-made for a specific model, I'd recommend you to expose a specific functions in your model that you would call in your view.

              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