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. Unable to save a list of object with the Settings QML type
Forum Updated to NodeBB v4.3 + New Features

Unable to save a list of object with the Settings QML type

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 761 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    tubbadu
    wrote on last edited by
    #1

    Hello, I'm trying to save the value of a variable, which is a list of object:

    let x = [
       {
    	"text": "hello"
       },
       {
    	"text": "world"
       }
    ]
    

    so I try to save it using the Settings QML type:

    Settings{
    	id: data
    }
    
    data.setValue("myKey", x)
    

    but this throws this error: QDataStream::operator<< was to save a non-trivial QJSValue. This is not supported anymore, please stream a QVariant instead.

    what does it mean? Do I need c++ to save this type of data? Isn't possible to convert my array to something like a string or something, save it, and then when reading the value reverting to the original array?

    saving integers, doubles and strings works well

    I'm using Qt 6.4.1 on Fedora Linux 37 KDE Spin, compiling for Desktop

    thank you in advance!

    JonBJ 1 Reply Last reply
    0
    • T tubbadu

      Hello, I'm trying to save the value of a variable, which is a list of object:

      let x = [
         {
      	"text": "hello"
         },
         {
      	"text": "world"
         }
      ]
      

      so I try to save it using the Settings QML type:

      Settings{
      	id: data
      }
      
      data.setValue("myKey", x)
      

      but this throws this error: QDataStream::operator<< was to save a non-trivial QJSValue. This is not supported anymore, please stream a QVariant instead.

      what does it mean? Do I need c++ to save this type of data? Isn't possible to convert my array to something like a string or something, save it, and then when reading the value reverting to the original array?

      saving integers, doubles and strings works well

      I'm using Qt 6.4.1 on Fedora Linux 37 KDE Spin, compiling for Desktop

      thank you in advance!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @tubbadu
      I don't use QML, but as you say if you just want to store the JSON as a string does x.toString() (JS) work/give a correct string to save?

      T 1 Reply Last reply
      0
      • JonBJ JonB

        @tubbadu
        I don't use QML, but as you say if you just want to store the JSON as a string does x.toString() (JS) work/give a correct string to save?

        T Offline
        T Offline
        tubbadu
        wrote on last edited by
        #3

        I finally achieved to save it using JSON.stringfy to convert it to string and JSON.parse to convert it back to list of object
        but is this the correct way of handling this? or is this just a workaround?

        JonBJ 1 Reply Last reply
        0
        • T tubbadu

          I finally achieved to save it using JSON.stringfy to convert it to string and JSON.parse to convert it back to list of object
          but is this the correct way of handling this? or is this just a workaround?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @tubbadu
          These are fine to use in JavaScript. Whether that is optimal here I don't know.

          Going back to the original error, is it telling you that it wanted you to go:

          data.setValue("myKey", x.toVariant())
          

          per https://doc.qt.io/qt-6/qjsvalue.html#toVariant-1?

          T 1 Reply Last reply
          0
          • JonBJ JonB

            @tubbadu
            These are fine to use in JavaScript. Whether that is optimal here I don't know.

            Going back to the original error, is it telling you that it wanted you to go:

            data.setValue("myKey", x.toVariant())
            

            per https://doc.qt.io/qt-6/qjsvalue.html#toVariant-1?

            T Offline
            T Offline
            tubbadu
            wrote on last edited by
            #5

            @JonB said in Unable to save a list of object with the Settings QML type:

            data.setValue("myKey", x.toVariant())

            thank you very very much!

            1 Reply Last reply
            0
            • N Offline
              N Offline
              newlearner
              wrote on last edited by
              #6

              hello, i wonder why toVariant() dont work for me,toVariant xxx is not a function

              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