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. Obtain object type string from QML

Obtain object type string from QML

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 8.3k 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.
  • I Offline
    I Offline
    Ildar
    wrote on last edited by
    #1

    Hello! How I can obtain type of a object from QML code?
    I need this feature in Loader:

    @Loader {
    sourceComponent: myObj.type === "MyObject" ? delegate1 : delegate2
    }@

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nologinma
      wrote on last edited by
      #2

      The correct form is the following:

      @a = (b==0)?1:0;@

      but in you example I see 3 '='

      In any case you cannot use == for the string.

      The correct code is the following (I suppose that myObj.type return a string):

      @QString mystring;

      mystring(myObj.type);

      mystring.compare("MyObject") == 0 ? delegate1 : delegate2;@

      in your case will be the following:

      @Loader {
      sourceComponent: myObj.type.compare("MyObject") == 0 ? delegate1 : delegate2
      }
      @

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Ildar
        wrote on last edited by
        #3

        Thank you for reply!
        You may read about tripple equals (===) in docs: "Comparison operators":http://www.c-point.com/javascript_tutorial/jsgrpComparison.htm

        Main problem is obtaining type information about a object from QML. I don't know how do it! In documentation about QObject ("QObject":http://qt-project.org/doc/qt-5/QObject.html) exists only one property - objectName, but metaObject() is not available under QML.

        I use small hack:
        In my objects i add new property:
        @Q_PROPERTY(QString className READ className CONSTANT)

        QString className()
        {
        return metaObject()->className();
        }
        @

        And in QML:
        @ListView {
        model: myModel
        delegate: Component {
        property bool isObject1: className === "MyObject1"

                    Text {
                        text: isObject1 ? property1 : property2
                    }
            }
        }@
        

        This is ugly, but it is works.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Ildar
          wrote on last edited by
          #4

          bq. In any case you cannot use == for the string.

          Why ?

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nologinma
            wrote on last edited by
            #5

            bq. You may read about tripple equals (===) in docs: Comparison operators [c-point.com]

            you are right! I never stop learning.

            I developed the following example:

            @import QtQuick 2.2
            import QtQuick.Controls 1.1

            ApplicationWindow {
            visible: true
            width: 2560
            height: 1600
            color: "white"

            Rectangle {
                width: 200
                height: 100
                color: "red"
            
                Text {
                    id: mytext
                    objectName: "MyTextObject"
                    anchors.centerIn: parent
                    text: "Hello, World!"
                }
            
                MouseArea {
                    anchors.fill: parent
                    onClicked:
                        {
                         parent.color = "blue"
                         mytext.text = mytext.objectName == "MyTextObject" ? "YES" : "NO"
                        }
                }
            }
            

            }@

            I get the name of the object by objectName method:

            @mytext.objectName@

            in your case you can use:

            @Loader {
            sourceComponent: myObj.objectName === "MyObject" ? delegate1 : delegate2
            }@

            but you have to assign to your object objectName = "MyObject"

            1 Reply Last reply
            0
            • I Offline
              I Offline
              Ildar
              wrote on last edited by
              #6

              So, this is also possible, but objectName is instance specific, not a type specific. It is more suitable to use objectName for other purposes, IMHO.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nologinma
                wrote on last edited by
                #7

                Please share your code about MyObject1 object just to understand better....

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Ildar
                  wrote on last edited by
                  #8

                  I mean that if we using objectName() method that we must set this property to all instances of class in constructor or in other places. For example:
                  @
                  MyObject1()
                  {
                  // setObjectName("MyObject1");
                  // or
                  setObjectName(metaObject()->className());
                  }
                  @

                  or as you wrote:
                  @
                  Text {
                  id: mytext
                  objectName: "MyTextObject"
                  anchors.centerIn: parent
                  text: "Hello, World!"
                  }
                  @

                  But if we using className wrapper property than we may define this methods in our class declaration. And this is allow to us to making safe inheritance from MyObject1: for subclasses className() return actual className!
                  @Q_PROPERTY(QString className READ className CONSTANT)

                  QString className()
                  {
                  return metaObject()->className();
                  }@

                  and we can using objectName for other purposes, for example:
                  @
                  MyObject1 objA = new MyObject1();
                  objA.setObjectName("A");

                  MyObject1 objB = new MyObject1();
                  objB.setObjectName("B");
                  @

                  This is can be useful for debugging.

                  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