Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-93201

instanceof operator works wrong for own components.

    XMLWordPrintable

Details

    • Windows

    Description

      The `instanceof` operator does not provide the correct result for the following case.

      In this case an own custom component is created and the name of the component is equal to an Quick Control like my own ToolButton in my module `./MyControls`:

      import QtQuick 2.15
      import QtQuick.Controls 2.15
      import QtQuick.Templates 2.15 as T
      
      T.ToolButton {
          id: root
      
          text: "Foo"
          checkable: true
          implicitWidth: 150
          implicitHeight: 50
          background: Rectangle 
          {
              color: root.down ? "red" : "green"
          }
          contentItem: Label {
              text: "%1".arg(root.text)
              horizontalAlignment: Qt.AlignHCenter
              verticalAlignment: Qt.AlignVCenter
          }
      }
      

      Next I am using my own ToolButton like this:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.15
      
      import MyControls 1.0
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          ToolButton {
              id: fooButton
      
              anchors.centerIn: parent
      
              width: 150
              height: 50
              visible: true
          }
          Label {
              anchors.top: fooButton.bottom
              width: 300
              height: 50
              text: "fooButton instanceof ToolButton => %1".arg(fooButton instanceof ToolButton)
              background: Rectangle {
                  color: "yellow"
              }
          }
      }
      

      Here the instanceof operator returns 0 for `fooButton instanceof ToolButton` in the text of the `outputLabel`.

      Using aliases for imports can be a workaround for this problem but makes code more complex.

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            cherold Christian von Wascinski
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes