Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Invalid alias reference. Unable to find id

    QML and Qt Quick
    settings alias property qml qt quick
    3
    9
    7842
    Loading More Posts
    • 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.
    • M
      mbnoimi last edited by

      Hi,

      I always get this error message qrc:/Page1.qml:8 Invalid alias reference. Unable to find id "textField1" whenever I run my app

      What's wrong with my code?

      NOTE: Please forgive me for silly question because I'm still a newbie in QML

      Page1Form.ui.qml

      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3
      
      Item {
          property alias textField1: textField1
          property alias button1: button1
      
          RowLayout {
              anchors.horizontalCenter: parent.horizontalCenter
              anchors.topMargin: 20
              anchors.top: parent.top
      
              TextField {
                  id: textField1
                  placeholderText: qsTr("Text Field")
              }
      
              Button {
                  id: button1
                  text: qsTr("Press Me")
              }
          }
      }
      

      Page1.qml

      import QtQuick 2.7
      import Qt.labs.settings 1.0
      
      Page1Form {
      
          Settings {
              id: settings
              property alias settings_host: textField1.text
          }
      
          button1.onClicked: {
              console.log("Button Pressed. Entered text: " + textField1.text);
          }
      }
      

      main.qml

      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3
      
      ApplicationWindow {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          SwipeView {
              id: swipeView
              anchors.fill: parent
              currentIndex: tabBar.currentIndex
      
              Page1 {
              }
      
              Page {
                  Label {
                      text: qsTr("Second page")
                      anchors.centerIn: parent
                  }
              }
          }
      
          footer: TabBar {
              id: tabBar
              currentIndex: swipeView.currentIndex
              TabButton {
                  text: qsTr("First")
              }
              TabButton {
                  text: qsTr("Second")
              }
          }
      }
      
      Yashpal 1 Reply Last reply Reply Quote 0
      • Yashpal
        Yashpal @mbnoimi last edited by

        @mbnoimi Have a look at property alias from http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html

        It says, 'aliasing an aliasing property will result in an error'.

        M 1 Reply Last reply Reply Quote 1
        • M
          mbnoimi @Yashpal last edited by mbnoimi

          @Yashpal Thanks

          But when I move Settings { property alias settings_host: textField1.text } from Page1.qml to Page1Form.ui.qml it works fine without any problem!

          NOTE: Basically I asked this question because I want to define Settings in Page1.qml instead of Page1Form.ui.qml

          Page1.qml

          import QtQuick 2.7
          
          Page1Form {
          
              button1.onClicked: {
                  console.log("Button Pressed. Entered text: " + textField1.text);
              }
          }
          

          Page1Form.ui.qml

          import QtQuick 2.7
          import QtQuick.Controls 2.0
          import QtQuick.Layouts 1.3
          import Qt.labs.settings 1.0
          
          Item {
              property alias textField1: textField1
              property alias button1: button1
          
              Settings { property alias settings_host: textField1.text }
          
          
              RowLayout {
                  anchors.horizontalCenter: parent.horizontalCenter
                  anchors.topMargin: 20
                  anchors.top: parent.top
          
                  TextField {
                      id: textField1
                      placeholderText: qsTr("Text Field")
                  }
          
                  Button {
                      id: button1
                      text: qsTr("Press Me")
                  }
              }
          }
          
          Yashpal 2 Replies Last reply Reply Quote 0
          • Yashpal
            Yashpal @mbnoimi last edited by

            @mbnoimi said in Invalid alias reference. Unable to find id:

            But when I move Settings { property alias settings_host: textField1.text } from Page1.qml to Page1Form.ui.qml it works fine without any problem!

            i think it's considering id 'textField1' over alias 'textField1', try to give different alias name and look how it works.

            M 1 Reply Last reply Reply Quote 1
            • Yashpal
              Yashpal @mbnoimi last edited by

              @mbnoimi said in Invalid alias reference. Unable to find id:

              NOTE: Basically I asked this question because I want to define Settings in Page1.qml instead of Page1Form.ui.qml

              Yes, you can do it. Make sure you don't use aliasing an aliasing property.

              1 Reply Last reply Reply Quote 1
              • M
                mbnoimi @Yashpal last edited by mbnoimi

                @Yashpal

                try to give different alias name and look how it works.

                I did as you suggested but nothing changed (see below plz)!

                Make sure you don't use aliasing an aliasing property.

                I can't use Settings without creating alias from alias which is a valid thing inside Page1Form.ui.qml but it's not inside Page1.qml!

                Page1Form.ui.qml

                import QtQuick 2.7
                import QtQuick.Controls 2.0
                import QtQuick.Layouts 1.3
                
                Item {
                    property alias button1: button1
                    property alias textFieldAlias: textFieldName
                
                    RowLayout {
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.topMargin: 20
                        anchors.top: parent.top
                
                        TextField {
                            id: textFieldName
                            placeholderText: qsTr("Text Field")
                        }
                
                        Button {
                            id: button1
                            text: qsTr("Press Me")
                        }
                    }
                }
                

                Page1.qml

                import QtQuick 2.7
                import Qt.labs.settings 1.0
                
                Page1Form {
                
                    Settings { property alias settings_host: textFieldName.text}
                
                    button1.onClicked: {
                        console.log("Button Pressed. Entered text: " + textFieldAlias.text);
                    }
                }
                

                Log

                QML debugging is enabled. Only use this in a safe environment.
                QQmlApplicationEngine failed to load component
                qrc:/main.qml:16 Type Page1 unavailable
                qrc:/Page1.qml:6 Invalid alias reference. Unable to find id "textFieldName"
                
                1 Reply Last reply Reply Quote 0
                • M
                  mbnoimi last edited by mbnoimi

                  Guys I still stuck with this problem since I posted it here :(

                  I'm not sure but it seems related to Settings type.

                  Can any one assure to me what if this issue a kind of bugs before report it as a bug?

                  E 1 Reply Last reply Reply Quote 0
                  • E
                    Eeli K @mbnoimi last edited by

                    @mbnoimi
                    Page1Form.ui.qml:

                    Item {
                        property alias text: textFieldName.text
                    

                    Page1.qml:

                    Page1Form {
                        id: page1
                        Settings { property alias settings_host: page1.text}
                    
                    M 1 Reply Last reply Reply Quote 2
                    • M
                      mbnoimi @Eeli K last edited by

                      That's it @Eeli-K
                      Thanks a lot

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post