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. How to update custom model data from C++ side or QML side ?
Forum Update on Tuesday, May 27th 2025

How to update custom model data from C++ side or QML side ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 4 Posters 1.4k 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.
  • M Offline
    M Offline
    Mohamad Ayrot
    wrote on 6 Jan 2023, 07:20 last edited by
    #1

    i have simple to do list Application, using custom model made in C++ by inheriting QAbstractListModel.

    i want to update each item Color or Text.
    i tried editing items from the View delegate (QML side) using color dialog, when i choose color from color dialog i want the item to be changed.
    i open color dialog using long press on the item.

    here is QML code for ToDoList

    what i need to do to update items color using color dialog ?

    
        property int item_index: 0
    
        ToDoModel{
            id: myModel
            getToDoList: todolist
    
        }
    
        Frame{
            Layout.fillWidth: true
            Layout.fillHeight: true
    
            ListView{
                id: toDoListView
                signal longPress(int index, string itemValue)
                clip: true
                implicitWidth: 250
                implicitHeight: 250
                anchors.fill: parent
                model: myModel
                spacing: 10
                delegate: Rectangle{
                    id: myDelegate
                    width: parent.width; height: 75
                    border {width: 2; color: 'lightblue'}
                    radius: 10
                    color:  ITEM_COLOR
                    CheckDelegate{
                        id: itemCheck
                        anchors.fill: parent
                        Text {
                            id: itemText
                            y: 29
                            text: model.ITEM_TEXT
                            anchors{left: parent.left; leftMargin: 23 }
                            font{
                                bold: model.ITEM_CHECKED ? true : false
                                strikeout: model.ITEM_CHECKED ? true : false
                            }
                        }
                        indicator: Rectangle{
                            implicitWidth: 26
                            implicitHeight: 26
                            anchors.right: parent.right
                            anchors.rightMargin: 25
                            x: itemCheck.leftPadding
                            y: parent.height / 2 - height / 2
                            radius: 3
                            border.color: model.ITEM_CHECKED ? "#FF0080" : "lightblue"
    
                            Rectangle {
                                width: 14
                                height: 14
                                x: 6
                                y: 6
                                radius: 2
                                color: itemCheck.down ? "#FF0080" : "#FF0080"
                                visible: model.ITEM_CHECKED
                            }
                        }
                        onCheckedChanged: {
                            model.ITEM_CHECKED = checked
                        }
                        onPressAndHold: {
                            item_index = index
                            chooseColorDialog.open()
                            print(index)
                            header.visible = true
                        }
    
                    }
                }
    
            }
        }
    
        ColorDialog{
            // TODO: change Color of item
            id: chooseColorDialog
            onAccepted: {
          
                header.visible = false
            }
            onRejected: {
                header.visible = false
            }
        }
    
    J 1 Reply Last reply 6 Jan 2023, 07:25
    0
    • M Mohamad Ayrot
      6 Jan 2023, 07:20

      i have simple to do list Application, using custom model made in C++ by inheriting QAbstractListModel.

      i want to update each item Color or Text.
      i tried editing items from the View delegate (QML side) using color dialog, when i choose color from color dialog i want the item to be changed.
      i open color dialog using long press on the item.

      here is QML code for ToDoList

      what i need to do to update items color using color dialog ?

      
          property int item_index: 0
      
          ToDoModel{
              id: myModel
              getToDoList: todolist
      
          }
      
          Frame{
              Layout.fillWidth: true
              Layout.fillHeight: true
      
              ListView{
                  id: toDoListView
                  signal longPress(int index, string itemValue)
                  clip: true
                  implicitWidth: 250
                  implicitHeight: 250
                  anchors.fill: parent
                  model: myModel
                  spacing: 10
                  delegate: Rectangle{
                      id: myDelegate
                      width: parent.width; height: 75
                      border {width: 2; color: 'lightblue'}
                      radius: 10
                      color:  ITEM_COLOR
                      CheckDelegate{
                          id: itemCheck
                          anchors.fill: parent
                          Text {
                              id: itemText
                              y: 29
                              text: model.ITEM_TEXT
                              anchors{left: parent.left; leftMargin: 23 }
                              font{
                                  bold: model.ITEM_CHECKED ? true : false
                                  strikeout: model.ITEM_CHECKED ? true : false
                              }
                          }
                          indicator: Rectangle{
                              implicitWidth: 26
                              implicitHeight: 26
                              anchors.right: parent.right
                              anchors.rightMargin: 25
                              x: itemCheck.leftPadding
                              y: parent.height / 2 - height / 2
                              radius: 3
                              border.color: model.ITEM_CHECKED ? "#FF0080" : "lightblue"
      
                              Rectangle {
                                  width: 14
                                  height: 14
                                  x: 6
                                  y: 6
                                  radius: 2
                                  color: itemCheck.down ? "#FF0080" : "#FF0080"
                                  visible: model.ITEM_CHECKED
                              }
                          }
                          onCheckedChanged: {
                              model.ITEM_CHECKED = checked
                          }
                          onPressAndHold: {
                              item_index = index
                              chooseColorDialog.open()
                              print(index)
                              header.visible = true
                          }
      
                      }
                  }
      
              }
          }
      
          ColorDialog{
              // TODO: change Color of item
              id: chooseColorDialog
              onAccepted: {
            
                  header.visible = false
              }
              onRejected: {
                  header.visible = false
              }
          }
      
      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 6 Jan 2023, 07:25 last edited by
      #2

      @Mohamad-Ayrot said in How to update custom model data from C++ side or QML side ?:

      what i need to do to update items color using color dialog

      very simple, implement now the setData function of your model
      https://doc.qt.io/qt-5/qabstractitemmodel.html#setData
      https://doc.qt.io/qt-6/qabstractitemmodel.html#setData

      and you're basically done :D


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mohamad Ayrot
        wrote on 6 Jan 2023, 08:14 last edited by
        #3

        Thank you for fast reply.
        i implemented setData function in QML as follows
        in ColorDialog
        but didn't work , is there something wrong with the function ?

                   ColorDialog{
                          // TODO: change Color of item
                          id: chooseColorDialog
                          onAccepted: {
                              myModel.setData(myModel.index(myIndex, 0), chooseColorDialog.color.toString(), ToDoModel.ColorRole)
                              header.visible = false
                          }
        
        
        J 1 Reply Last reply 6 Jan 2023, 08:52
        0
        • M Mohamad Ayrot
          6 Jan 2023, 08:14

          Thank you for fast reply.
          i implemented setData function in QML as follows
          in ColorDialog
          but didn't work , is there something wrong with the function ?

                     ColorDialog{
                            // TODO: change Color of item
                            id: chooseColorDialog
                            onAccepted: {
                                myModel.setData(myModel.index(myIndex, 0), chooseColorDialog.color.toString(), ToDoModel.ColorRole)
                                header.visible = false
                            }
          
          
          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 6 Jan 2023, 08:52 last edited by
          #4

          @Mohamad-Ayrot some basic questions
          do you emit dataChanged signal in your setData function ?
          also a delegate should be able to call setData implicitly for its index via model.roleName = value


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mohamad Ayrot
            wrote on 6 Jan 2023, 10:06 last edited by
            #5

            this is setData function

            bool ToDoModel::setData(const QModelIndex &index, const QVariant &value, int role)
            {
            //        if (data(index, role) != value) {
            //            // FIXME: Implement me!
            //            emit dataChanged(index, index, {role});
            //            return true;
            //        }
            //        return false;
                if(!m_todolist)
                    return false;
                Item item = m_todolist->getItems().at(index.row());
                switch(role){
                case ColorRole:
                    item.itemColor() = value.toString();
                    break;
                case TextRole:
                    item.itemText() = value.toString();
                    break;
                case CheckedRole:
                    if(item.itemChecked() != value.toBool()){
                        item.setChecked(value.toBool());
                    }
                    break;
                }
                if (m_todolist->setItemAt(index.row(), item)) {
                    emit dataChanged(index, index, QVector<int>() << role);
                    return true;
                }
                return false;
            
            }
            
            
            1 Reply Last reply
            0
            • oria66O Offline
              oria66O Offline
              oria66
              wrote on 6 Jan 2023, 12:47 last edited by
              #6

              You should add dataChanged(...) before break sentence.

              The truth is out there

              1 Reply Last reply
              0
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on 6 Jan 2023, 14:48 last edited by
                #7

                define item color inside myModel and assign it to delegate. Simply update the color with index in myModel with signal longPress. That will do it.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mohamad Ayrot
                  wrote on 6 Jan 2023, 21:32 last edited by
                  #8

                  @JoeCFD can you write it as a code please ?

                  1 Reply Last reply
                  0

                  1/8

                  6 Jan 2023, 07:20

                  • Login

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