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. Binding based on typeof doesn't work any more

Binding based on typeof doesn't work any more

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 4 Posters 1.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.
  • SeDiS Offline
    SeDiS Offline
    SeDi
    wrote on last edited by
    #1

    Hi,
    I have a simple Label that has to show a property of a rootProperty, the latter not being created when the qml is loaded/created. Quite frequently I use this construct and it did work perfectly in older Qt versions (something before 12.1 - that's the earliest I have installed):

    //[main.qml, inside a Label]
    text: typeof Controller === "undefined" ? "" : Controller.successfulSteps
    

    Now this doesn't show anything anymore. The following workaround does show the desired result:

    property string dummy: ""
    text: dummy+(typeof Controller === "undefined" ? "" : Controller.successfulSteps)
    

    This seems very hackish. Is there a better way to achieve this? Why has the behavior been changed at all in the first place?
    The Controller has been set as a root property after loading the QML page:

    //[main.cpp]
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    Controller controller(&engine);
    engine.rootContext()->setContextProperty("Controller", QVariant::fromValue<Controller*>(&controller));
    
    

    BR
    Sebastian

    JKSHJ 1 Reply Last reply
    0
    • SeDiS SeDi

      Hi,
      I have a simple Label that has to show a property of a rootProperty, the latter not being created when the qml is loaded/created. Quite frequently I use this construct and it did work perfectly in older Qt versions (something before 12.1 - that's the earliest I have installed):

      //[main.qml, inside a Label]
      text: typeof Controller === "undefined" ? "" : Controller.successfulSteps
      

      Now this doesn't show anything anymore. The following workaround does show the desired result:

      property string dummy: ""
      text: dummy+(typeof Controller === "undefined" ? "" : Controller.successfulSteps)
      

      This seems very hackish. Is there a better way to achieve this? Why has the behavior been changed at all in the first place?
      The Controller has been set as a root property after loading the QML page:

      //[main.cpp]
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      Controller controller(&engine);
      engine.rootContext()->setContextProperty("Controller", QVariant::fromValue<Controller*>(&controller));
      
      

      BR
      Sebastian

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @SeDi said in Binding based on typeof doesn't work any more:

      Quite frequently I use this construct and it did work perfectly in older Qt versions

      ...

      Now this doesn't show anything anymore.

      ...

      This seems very hackish. Is there a better way to achieve this? Why has the behavior been changed at all in the first place?

      Hi Sebastian,

      I'm not sure if the change was intended or if it's a bug in newer versions. The Qt engineers in the Interest mailing list (https://lists.qt-project.org/listinfo/interest ) could provide some insights or provide fix -- subscribe to the list and ask there.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1
      • IntruderExcluderI Offline
        IntruderExcluderI Offline
        IntruderExcluder
        wrote on last edited by
        #3

        @SeDi said in Binding based on typeof doesn't work any more:

        This seems very hackish. Is there a better way to achieve this?

        Why just not to write

        text: Controller ? Controller.successfulSteps : ""
        
        1 Reply Last reply
        0
        • SeDiS Offline
          SeDiS Offline
          SeDi
          wrote on last edited by
          #4

          @IntruderExcluder : Thanks for the idea. I can't, because this leads to the qml warning/error

          ReferenceError: Controller is not defined
          

          as long this (Controller not yet defined) is the case, as undefined does not mean Null. Plus it also doesn't show a result.

          @JKSH : Thanks, I've done that now! I'll come back with the final answer.

          1 Reply Last reply
          0
          • IntruderExcluderI Offline
            IntruderExcluderI Offline
            IntruderExcluder
            wrote on last edited by
            #5

            Than another ugly variant, but without dummy property:

            text: {
                try {
                    return Controller.successfulSteps;
                } catch (e) {
                    return "";
                }
            }
            
            1 Reply Last reply
            1
            • SeDiS Offline
              SeDiS Offline
              SeDi
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • SeDiS Offline
                SeDiS Offline
                SeDi
                wrote on last edited by
                #7

                Thanks for your suggestions!

                Unfortunately it's still either no binding or error message, if I don't use a dummy property. [At first I had though this to work, but that was a mistake on my side]

                But by now I know the problem. It is actually a bug that was introduced with 5.11->5.12 and has been fixed in 5.12.5 / 5.13.1.
                I've had tested it with 5.12.3 and 13.0 :-)

                Here's the bug:
                https://bugreports.qt.io/browse/QTBUG-76796
                Here's the fix:
                https://codereview.qt-project.org/c/qt/qtdeclarative/+/266776

                The best solution, of course, would be to load the QML page after the contextProperty is set to the engine. I can and will do that with my current and future code.

                For my legacy code this bug would have meant a huge amount of work - or an almost equal amount of (ugly) patching. I am very happy, that this bug has been fixed! I will just upgrade.

                Thank you for time, effort and creative input!

                J.HilkJ 1 Reply Last reply
                3
                • SeDiS SeDi

                  Thanks for your suggestions!

                  Unfortunately it's still either no binding or error message, if I don't use a dummy property. [At first I had though this to work, but that was a mistake on my side]

                  But by now I know the problem. It is actually a bug that was introduced with 5.11->5.12 and has been fixed in 5.12.5 / 5.13.1.
                  I've had tested it with 5.12.3 and 13.0 :-)

                  Here's the bug:
                  https://bugreports.qt.io/browse/QTBUG-76796
                  Here's the fix:
                  https://codereview.qt-project.org/c/qt/qtdeclarative/+/266776

                  The best solution, of course, would be to load the QML page after the contextProperty is set to the engine. I can and will do that with my current and future code.

                  For my legacy code this bug would have meant a huge amount of work - or an almost equal amount of (ugly) patching. I am very happy, that this bug has been fixed! I will just upgrade.

                  Thank you for time, effort and creative input!

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @SeDi don't forget to use the topic tools to set the topic to solved than😉


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  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