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.
  • S Offline
    S Offline
    Stefan Monov76
    wrote on 9 Jan 2017, 21:29 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?

    R 1 Reply Last reply 9 Jan 2017, 21:43
    0
    • S Stefan Monov76
      9 Jan 2017, 21:29

      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?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 9 Jan 2017, 21:43 last edited by raven-worx 1 Sept 2017, 21:46
      #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

      S 1 Reply Last reply 10 Jan 2017, 08:23
      2
      • R raven-worx
        9 Jan 2017, 21:43

        @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,

        S Offline
        S Offline
        Stefan Monov76
        wrote on 10 Jan 2017, 08:23 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.

        R G 2 Replies Last reply 10 Jan 2017, 08:29
        0
        • S Stefan Monov76
          10 Jan 2017, 08:23

          @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.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 10 Jan 2017, 08:29 last edited by raven-worx 1 Oct 2017, 08:29
          #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
          • S Stefan Monov76
            10 Jan 2017, 08:23

            @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.

            G Online
            G Online
            GrecKo
            Qt Champions 2018
            wrote on 10 Jan 2017, 08:40 last edited by GrecKo 1 Oct 2017, 08:41
            #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

            1/5

            9 Jan 2017, 21:29

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved