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. Converting object to string
Qt 6.11 is out! See what's new in the release blog

Converting object to string

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 5 Posters 16.5k 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.
  • B Offline
    B Offline
    Babalas
    wrote on last edited by
    #1

    What do I need to provide to be able to convert my object to a string in something like console.debug? At the moment, printing out my QObjectList gives me @KeyValue(0x196bdc0),KeyValue(0x18f8d40),KeyValue(0x18f90e0),KeyValue(0x1955d40)@
    I'd like to convert that to
    @KeyValue(0x196bdc0, Key="A", Value=1),KeyValue(0x18f8d40, Key="B", Value=2),KeyValue(0x18f90e0, Key="C", Value=3),KeyValue(0x1955d40, Key="D", Value=4)@

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

      In QML and C++, I often add ::toString() method to various classes to make them print well and easy. You can probably do the same, although it's hard to be exact without seeing your code.

      (Z(:^

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Babalas
        wrote on last edited by
        #3

        So is that an explicit call?
        In my example above I have a QObjectList model of KeyValue classes. Basically just pairs. The output I got was by doing
        @console.debug(model)@

        I'm guessing to produce something similar I'd have to loop and print item.toString then.

        Or to go off on a tangent I'm wondering if the javascript interpreter has some default stringify method that can be overridden similar to Rubys "inspect":http://ruby-doc.org/core-2.1.1/Object.html#method-i-inspect method

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

          I'm no expert on JavaScript, sorry.

          Yes, your thinking on .tsoString() is correct.

          (Z(:^

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            If you need the toString-method only for quick debugging in QML you can use a JS loop to go though the object and print all properties with their values in most cases. or write a special function if you want to print only some properties of course.

            It sometimes sucks that JS only prints [Object object] for objects by default :D
            here my solution:
            @
            // register this function once
            Object.prototype.print = function() {
            for (var p in this) {
            var v = this[p]
            if (typeof v === "object")
            v.print()
            else if (typeof v !== "function")
            console.log(p, v)
            }
            }

            // instead of console.log(obj) use
            obj.print()
            @
            that will recursively print all properties with their values to the console. That is just a simple JS function, you can modify that to your liking and print what you want. :)

            of course you can also write a c++ function and call it from QML if you prefer that.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Best method for me:

              console.debug(JSON.stringify(yourObject));

              JSON global object is part of ES5(EcmaScript 5), it's in core JS functionality.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vincent007
                wrote on last edited by
                #7

                Is there an easy way to print QML object by JavaScript?

                The following code does not work, and no response after being executed.

                @
                import QtQuick 2.1
                import QtQuick.Controls 1.0

                ApplicationWindow {
                id: root
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")

                menuBar: MenuBar {
                    Menu {
                        title: qsTr("File")
                        MenuItem {
                            text: qsTr("Exit")
                            onTriggered: Qt.quit();
                        }
                    }
                }
                
                Text {
                    id: mytext
                    text: qsTr("Hello World")
                    anchors.centerIn: parent
                }
                
                Component.onCompleted: {
                    console.debug(JSON.stringify(mytext))
                }
                

                }
                @

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Happened here too. Probably you should submit a bug report.
                  Doesn't happen to cpp exported object or js object

                  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