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. error: Unable to assign [undefined] to QString
Forum Updated to NodeBB v4.3 + New Features

error: Unable to assign [undefined] to QString

Scheduled Pinned Locked Moved Solved QML and Qt Quick
18 Posts 4 Posters 4.9k 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.
  • mranger90M mranger90

    You have at least one issue that could be causing this::

    in data()

        if (index.isValid()) {
            return QVariant();
        }
    

    I think you want to test against !index.isValid()

    Lucas_1603L Offline
    Lucas_1603L Offline
    Lucas_1603
    wrote on last edited by
    #5

    @mranger90 Oh, it worked like a charm! Thank you so much

    1 Reply Last reply
    0
    • Lucas_1603L Offline
      Lucas_1603L Offline
      Lucas_1603
      wrote on last edited by
      #6

      One more question, if I want to use PlaylistModel out side of ListView in QML, such as a Text item (it doesn't have model property to assign PlaylistModel as a model):

      PlaylistModel {
          id: model_
      }
      
      Text {
              id: audioTitle
              anchors.top: headerItem.bottom
              anchors.topMargin: 3
              anchors.left: mediaPlaylist.right
              anchors.leftMargin: 3
              text: model_.title
              color: "white"
              font.pixelSize: 15
          }
      

      But when I try to set model_.title to text, it didn't understand and raised the error: Unable to assign [undefined] to QString
      How can I use it properly?
      Thanks a lot ^^

      S 1 Reply Last reply
      0
      • Lucas_1603L Lucas_1603

        One more question, if I want to use PlaylistModel out side of ListView in QML, such as a Text item (it doesn't have model property to assign PlaylistModel as a model):

        PlaylistModel {
            id: model_
        }
        
        Text {
                id: audioTitle
                anchors.top: headerItem.bottom
                anchors.topMargin: 3
                anchors.left: mediaPlaylist.right
                anchors.leftMargin: 3
                text: model_.title
                color: "white"
                font.pixelSize: 15
            }
        

        But when I try to set model_.title to text, it didn't understand and raised the error: Unable to assign [undefined] to QString
        How can I use it properly?
        Thanks a lot ^^

        S Offline
        S Offline
        sharath
        wrote on last edited by sharath
        #7

        Hello @Lucas_1603,

        You can not use like that. if you want to use it in text then you need to loop over the model like below.

        function iterate(){
        for(var i = 0; i < yourModel.length; i++){
        console.log("model's title::"+yourModel[i].title);
        audioTitle.text=yourModel[i].title;
        }
        } 
        

        just try and let me know your result;

        Happy coding.

        Lucas_1603L 1 Reply Last reply
        0
        • S sharath

          Hello @Lucas_1603,

          You can not use like that. if you want to use it in text then you need to loop over the model like below.

          function iterate(){
          for(var i = 0; i < yourModel.length; i++){
          console.log("model's title::"+yourModel[i].title);
          audioTitle.text=yourModel[i].title;
          }
          } 
          

          just try and let me know your result;

          Happy coding.

          Lucas_1603L Offline
          Lucas_1603L Offline
          Lucas_1603
          wrote on last edited by
          #8

          @sharath I tried it out but it didn't work as we expected. I wrote a function like yours and called it in text but it still raised the error: Unable to assign [undefined] to QString
          Don't have any general methods to use PlaylistModel for both in ListView and out of it (wherever in QML)?
          Thanks for your help

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

            @Lucas_1603 said in error: Unable to assign [undefined] to QString:

                text: model_.title
            

            What should that mean?
            "Give me the title of the song in my playlist"?
            Which song are you talking about? the first, the last, one in the middle?

            Lucas_1603L 1 Reply Last reply
            1
            • GrecKoG GrecKo

              @Lucas_1603 said in error: Unable to assign [undefined] to QString:

                  text: model_.title
              

              What should that mean?
              "Give me the title of the song in my playlist"?
              Which song are you talking about? the first, the last, one in the middle?

              Lucas_1603L Offline
              Lucas_1603L Offline
              Lucas_1603
              wrote on last edited by
              #10

              @GrecKo The second song for example. The title is "Mysong2"

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

                So you have to somehow tell that you want the second one.

                You could add an invokable get method in your model return data from a specific row.
                But if you need it to be updated when the model changes, it's going to be hard.

                I wrote a library for that https://github.com/oKcerG/QmlModelHelper , it works for any standard QAbstractItemModel.

                You could use it like that:

                text: model_.ModelHelper.map(1).title

                1 Reply Last reply
                1
                • Lucas_1603L Lucas_1603

                  @GrecKo The second song for example. The title is "Mysong2"

                  S Offline
                  S Offline
                  sharath
                  wrote on last edited by sharath
                  #12

                  @Lucas_1603 for second song,

                  text:model_[1].title
                  
                  Lucas_1603L 1 Reply Last reply
                  0
                  • S sharath

                    @Lucas_1603 for second song,

                    text:model_[1].title
                    
                    Lucas_1603L Offline
                    Lucas_1603L Offline
                    Lucas_1603
                    wrote on last edited by
                    #13

                    @sharath @GrecKo thanks for your ideas, I got it.
                    But in case I want to display all titles in text, how can I do that?

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

                      What @sharath proposed won't work. You can't access QAbstractItemModel's data with [].

                      @Lucas_1603 please better define what you want.
                      Do you want multiple text items for each of your songs? Or do you want all titles concatenated in one text? Or something else?

                      Lucas_1603L 1 Reply Last reply
                      0
                      • Lucas_1603L Lucas_1603

                        @sharath @GrecKo thanks for your ideas, I got it.
                        But in case I want to display all titles in text, how can I do that?

                        S Offline
                        S Offline
                        sharath
                        wrote on last edited by
                        #15

                        @Lucas_1603 Create get method to get perticular index data from QAbstractListModel class. follow https://stackoverflow.com/questions/22711421/how-to-implement-qml-listmodel-like-get-method-for-an-qabstractlistmodel-derived .
                        i'm sure that your problem might solve with this link.

                        Happy coding

                        1 Reply Last reply
                        0
                        • GrecKoG GrecKo

                          What @sharath proposed won't work. You can't access QAbstractItemModel's data with [].

                          @Lucas_1603 please better define what you want.
                          Do you want multiple text items for each of your songs? Or do you want all titles concatenated in one text? Or something else?

                          Lucas_1603L Offline
                          Lucas_1603L Offline
                          Lucas_1603
                          wrote on last edited by
                          #16

                          @GrecKo In ListView, I just need to specify the model and then I can assign model_.title, it'll display all titles concatenated in one text. I want do the same from the Text outside of ListView.
                          @sharath I actually created get method and it worked as I expected. Now I want to display all titles concatenated in one text.

                          GrecKoG 1 Reply Last reply
                          0
                          • Lucas_1603L Lucas_1603

                            @GrecKo In ListView, I just need to specify the model and then I can assign model_.title, it'll display all titles concatenated in one text. I want do the same from the Text outside of ListView.
                            @sharath I actually created get method and it worked as I expected. Now I want to display all titles concatenated in one text.

                            GrecKoG Offline
                            GrecKoG Offline
                            GrecKo
                            Qt Champions 2018
                            wrote on last edited by
                            #17

                            @Lucas_1603 said in error: Unable to assign [undefined] to QString:

                            @GrecKo In ListView, I just need to specify the model and then I can assign model_.title, it'll display all titles concatenated in one text.

                            No that's what it does, it creates a delegate for each row and position them the one after the other

                            I want do the same from the Text outside of ListView.

                            If you want the same why don't you want a ListView.

                            Maybe do a schema of what you actually want?

                            Lucas_1603L 1 Reply Last reply
                            0
                            • GrecKoG GrecKo

                              @Lucas_1603 said in error: Unable to assign [undefined] to QString:

                              @GrecKo In ListView, I just need to specify the model and then I can assign model_.title, it'll display all titles concatenated in one text.

                              No that's what it does, it creates a delegate for each row and position them the one after the other

                              I want do the same from the Text outside of ListView.

                              If you want the same why don't you want a ListView.

                              Maybe do a schema of what you actually want?

                              Lucas_1603L Offline
                              Lucas_1603L Offline
                              Lucas_1603
                              wrote on last edited by
                              #18

                              @GrecKo Great, I created another ListView and everything was done ^^
                              Thank you a lot. How about a schema as you said, can you give me some links or docs to learn more about it? Thanks

                              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