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 make C++ qml model example editable?
QtWS25 Last Chance

How to make C++ qml model example editable?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 3 Posters 2.5k 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
    milan
    wrote on last edited by
    #1

    Hello, I am working on this example http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html. I added setData method to edit animal type using its role. But I see in debug mode, the program never breaks in setData method even though i added breakpoint. Nor, could I edit the animal type.

    bool AnimalModel ::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (role == TypeRole )
        {
            qDebug() << "Break here";
        }
        return true;
    }
    

    Please help.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Did you add any code on QML that would trigger a model change? Like, a TextInput or ComboBox that would allow the user to modify animal type?

      (Z(:^

      1 Reply Last reply
      1
      • M Offline
        M Offline
        milan
        wrote on last edited by
        #3

        @sierdzio . I used Text in QML now, like in the example.

        import QtQuick 2.0
        
        ListView {
            width: 200; height: 250
        
            model: myModel
            delegate: Text { text: "Animal: " + type + ", " + size }
        }
        
        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Well then you don't modify any model data, so setData() is never called.

          You need to modify type or size somewhere for setData() to be invoked.

          (Z(:^

          M 1 Reply Last reply
          3
          • sierdzioS sierdzio

            Well then you don't modify any model data, so setData() is never called.

            You need to modify type or size somewhere for setData() to be invoked.

            M Offline
            M Offline
            milan
            wrote on last edited by
            #5

            @sierdzio . I now replaced Text with TextInput, and I edited the text in the application. But it is still not triggered.

            Gojir4G 1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              You have probably modified text property of TextInput, but not type property of the model.

              Sth. like:

              TextInput { 
                text: type
                onEditingFinished: type = text
              }
              

              (Z(:^

              M 1 Reply Last reply
              3
              • sierdzioS sierdzio

                You have probably modified text property of TextInput, but not type property of the model.

                Sth. like:

                TextInput { 
                  text: type
                  onEditingFinished: type = text
                }
                
                M Offline
                M Offline
                milan
                wrote on last edited by
                #7

                @sierdzio said in How to make C++ qml model example editable?:

                onEditingFinished: type = text

                Thanks a lot. This triggered the setData. I also need to trigger the setData using checkRole i have added. So, there is checkbox in qml which is a boolean in C++ model. I would like this to trigger the setData method too.

                1 Reply Last reply
                1
                • M milan

                  @sierdzio . I now replaced Text with TextInput, and I edited the text in the application. But it is still not triggered.

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

                  @milan As @sierdzio said, you need to modify type or size. It will much simpler if you use two separated input control for each of the data to set.

                  Something like this maybe:

                  delegate: Row{
                      spacing: 0
                      Text{ text: "Animal: "}
                      TextInput { text: type 
                          onEditingFinished: type = text
                      }
                      Text { text: ", " }
                      TextInput { text: size
                          onEditingFinished: size = text
                      }
                  }
                  
                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    milan
                    wrote on last edited by
                    #9

                    I did similar to checkbox too, it works now. Thanks @sierdzio a lot.

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      Happy coding :-)

                      (Z(:^

                      1 Reply Last reply
                      2

                      • Login

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