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. Usage of setContextProperty

Usage of setContextProperty

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 4.9k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    I have used a QString variable inside setContextProperty, but it says that "must be a string literal to be available in the QMl editor".
    example:
    @QString strQmlObjName = "identifier";
    setContextProperty(strQmlObjName, obj);@

    Ansif

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lack
      wrote on last edited by
      #2

      That's strange.. does that work with just
      setContextProperty("identifier", obj); ?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        [quote author="lack" date="1400226114"]That's strange.. does that work with just
        setContextProperty("identifier", obj); ?[/quote]

        Hello,

        Sorry for the late reply. Yes this is an example. I wanted to know whether this will work in the way that i have shown here.

        Ansif

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sreich
          wrote on last edited by
          #4

          qml editor? are you using some kind of qml editor?

          because that code is entirely valid, all things otherwise.

          Software Developer for KDE

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            [quote author="sreich" date="1400385106"]qml editor? are you using some kind of qml editor?

            because that code is entirely valid, all things otherwise.[/quote]

            Hi,

            Let me try to find a solution for this problem. Might be this is not a valid question.

            Ansif

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lack
              wrote on last edited by
              #6

              Got it. I ran into the same problem trying to extend QtQuick2ControlsApplicationViewer that is created by default when you create a qt quick application on Qt Creator 3.0.1

              By string literal, it meant plain created on the spot "string", not from a variable.

              so
              @
              setContextProperty("this_works", &aQobject);
              @

              instead of

              @
              QString notAStringLiteral = "this_wont_work";
              setContextProperty(notAStringLiteral, &aQObject);
              @

              My guess the problem is in variable ownership/reference. QStrings are COW, they are not easily copied/cloned. If the variable later changed in c++, who knows what's gonna happen on QML side?

              But they do provide a solution to this,
              @
              QString notAStringLiteral = "now_this_works";
              setContextProperty(QStringLiteral(notAStringLiteral), &aQObject);
              @
              From the docs:

              [quote]
              QStringLiteral(str)
              The macro generates the data for a QString out of str at compile time if the compiler supports it. Creating a QString from it is free in this case, and the generated string data is stored in the read-only segment of the compiled object file.
              [/quote]

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Sorry for the delay. I think the above soln will work only in Unicode enabled project.

                1 Reply Last reply
                0
                • jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #8

                  That's curious. What version of Qt, and what platform and compiler is this with?

                  Is this message reported by Creator during editing, by the compiler as a warning or error, or by the QML runtime?

                  This works for me:
                  @
                  QQmlApplicationEngine engine;
                  QString str("Obj");
                  Object obj;
                  engine.rootContext()->setContextProperty(str, &obj);
                  @

                  I don't see anything that resembles “must be a string literal to be available in the QMl editor” using Creator 3.1.0 and Qt 5.3.0 on Linux/XCB using g++ 4.8.2.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lack
                    wrote on last edited by
                    #9

                    @jeremy, I'm on win7-x64, Qt 5.2/msvc 2013 x64, Qt creator 3.0.1
                    I also observed this behaviour.
                    It's not a warning. In my case the variable was underlined and when you hover the tooltip says 'must be string literal ...'

                    1 Reply Last reply
                    0
                    • jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #10

                      Thanks for the clarification.

                      The message indicates that Creator won't be able to highlight the context property's name or provide completion when editing QML source within the project. It has no effect on the runtime.

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        Yes you are right. Qt creator is not able to highlight the context property's name during compile time. But in runtime it works fine. I was trying to find a solution to remove this warning during compile time. But still i didn't find a right soln. I think usage of QStringLiteral will solve this issue in unicode enabled settings.

                        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