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. QML: How to access context property if its name is shadowed by a component-local property?

QML: How to access context property if its name is shadowed by a component-local property?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
syntaxpropertiesshadowing
5 Posts 3 Posters 3.2k Views
  • 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.
  • Stefan Monov76S Offline
    Stefan Monov76S Offline
    Stefan Monov76
    wrote on last edited by
    #1

    My code:

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    int main(int argc, char *argv[]) {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        engine.rootContext()->setContextProperty("text", "hey");
    
        return app.exec();
    }
    

    main.qml:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    
    Window {
        id: window
        visible: true
        width: 640
        height: 480
    
        Text {
            text: text
        }
    }
    

    Of course, the text: text line doesn't do what I want it to, because of name shadowing.

    I worked around that by setting a property on the root object, rather than on the root context, and using text: window.text.

    Is there any real fix though?

    raven-worxR 1 Reply Last reply
    0
    • Stefan Monov76S Stefan Monov76

      My code:

      main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      
      int main(int argc, char *argv[]) {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
      
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          engine.rootContext()->setContextProperty("text", "hey");
      
          return app.exec();
      }
      

      main.qml:

      import QtQuick 2.5
      import QtQuick.Window 2.2
      
      Window {
          id: window
          visible: true
          width: 640
          height: 480
      
          Text {
              text: text
          }
      }
      

      Of course, the text: text line doesn't do what I want it to, because of name shadowing.

      I worked around that by setting a property on the root object, rather than on the root context, and using text: window.text.

      Is there any real fix though?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Stefan-Monov76
      short answer: no
      Obviously such case is semantically ambigious.

      You can use an alias property, which is basically the same as your solution,

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Stefan Monov76S 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @Stefan-Monov76
        short answer: no
        Obviously such case is semantically ambigious.

        You can use an alias property, which is basically the same as your solution,

        Stefan Monov76S Offline
        Stefan Monov76S Offline
        Stefan Monov76
        wrote on last edited by
        #3

        @raven-worx: Yes, it's ambiguous because I don't know (or there isn't) a syntax for disambiguating. For example in C++ a roughly equivalent statement, involving a local text and a global text, would be text = ::text which is unambiguous. Or, if you have a member var and a local, you could use text = this->text. So this is possible in C++, I was just asking if it's possible in QML as well :)

        Thanks for the answer.

        raven-worxR GrecKoG 2 Replies Last reply
        0
        • Stefan Monov76S Stefan Monov76

          @raven-worx: Yes, it's ambiguous because I don't know (or there isn't) a syntax for disambiguating. For example in C++ a roughly equivalent statement, involving a local text and a global text, would be text = ::text which is unambiguous. Or, if you have a member var and a local, you could use text = this->text. So this is possible in C++, I was just asking if it's possible in QML as well :)

          Thanks for the answer.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Stefan-Monov76
          This is because C++ is object-orientated and type-safe. So namespaces are supported there.
          Whereas JavaScript is not, thus it's not supported/needed there.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • Stefan Monov76S Stefan Monov76

            @raven-worx: Yes, it's ambiguous because I don't know (or there isn't) a syntax for disambiguating. For example in C++ a roughly equivalent statement, involving a local text and a global text, would be text = ::text which is unambiguous. Or, if you have a member var and a local, you could use text = this->text. So this is possible in C++, I was just asking if it's possible in QML as well :)

            Thanks for the answer.

            GrecKoG Online
            GrecKoG Online
            GrecKo
            Qt Champions 2018
            wrote on last edited by GrecKo
            #5

            @raven-worx: Yes, it's ambiguous because I don't know (or there isn't) a syntax for disambiguating. For example in C++ a roughly equivalent statement, involving a local text and a global text, would be text = ::text which is unambiguous.

            Hehe, I was discussing that with others last week, a way to differentiate context properties from regular object properties. Could be used in your case to disambiguate the root context property, or to access model properties from outside a delegate, for example : listView.currentItem::age without having to expose it as a property.
            This syntax is not achievable for the moment, but you could do something like this listView.currentItem.Context.property("age"), you won't get notified of any change though. You could also do it for the root context with a singleton exposing it with the same caveat about the changes.

            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