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. Using same constants in QML Scripts and C++
Forum Update on Monday, May 27th 2025

Using same constants in QML Scripts and C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 6 Posters 2.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.
  • F Offline
    F Offline
    FabQ
    wrote on 11 Sept 2018, 13:59 last edited by
    #1

    Hello,

    I have started a QML application and I would like to use same constants in QML scripts AND in C++ part.
    I will have 50 to 100 constants (most of them will be some strings) to define.
    What are possibilities to do it ?

    Thanks.

    O J D 3 Replies Last reply 11 Sept 2018, 18:49
    0
    • F FabQ
      11 Sept 2018, 13:59

      Hello,

      I have started a QML application and I would like to use same constants in QML scripts AND in C++ part.
      I will have 50 to 100 constants (most of them will be some strings) to define.
      What are possibilities to do it ?

      Thanks.

      O Offline
      O Offline
      ODБOï
      wrote on 11 Sept 2018, 18:49 last edited by
      #2

      @FabQ hi
      this is 6 years old but still can help https://forum.qt.io/topic/19557/solved-exporting-functions-and-constants-from-c-to-qml

      might i ask you what to use these constants ?

      1 Reply Last reply
      0
      • F FabQ
        11 Sept 2018, 13:59

        Hello,

        I have started a QML application and I would like to use same constants in QML scripts AND in C++ part.
        I will have 50 to 100 constants (most of them will be some strings) to define.
        What are possibilities to do it ?

        Thanks.

        J Online
        J Online
        J.Hilk
        Moderators
        wrote on 12 Sept 2018, 05:20 last edited by
        #3

        @FabQ

        I think, what you're looking for is the QGlobalStatic Class, To access it in QML, you should make it available via

        rootContext()->setContextProperty("Name",Instance Pointer);
        This this link for more information


        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
        3
        • G Offline
          G Offline
          GPBeta
          wrote on 12 Sept 2018, 08:17 last edited by
          #4

          Please consider using the Q_ENUM tag:

          class Constants : public QObject {
              Q_OBJECT
          public:
          
              enum ConstantValues{ MinValue = 0, MaxValue = 65535 };
              Q_ENUM(ConstantValues)
          
          };
          

          Now you can access Constants::MinValue with C++ and Constants.MinValue with QML.

          \BAKA BAKA/

          1 Reply Last reply
          3
          • F FabQ
            11 Sept 2018, 13:59

            Hello,

            I have started a QML application and I would like to use same constants in QML scripts AND in C++ part.
            I will have 50 to 100 constants (most of them will be some strings) to define.
            What are possibilities to do it ?

            Thanks.

            D Offline
            D Offline
            Diracsbracket
            wrote on 12 Sept 2018, 09:19 last edited by
            #5

            @FabQ
            To add to what @GPBeta wrote, this requires you to register the Constants class

             qmlRegisterType<Constants>("your.module", 0, 1, "Constants");
            

            http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#enumeration-types

            1 Reply Last reply
            2
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 12 Sept 2018, 12:34 last edited by
              #6

              All solutions revolve around registering the class with QML. You can consider using Q_GADGET, if you don't want to inherit from QObject while exposing constants to QML.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              4
              • F Offline
                F Offline
                FabQ
                wrote on 12 Sept 2018, 14:52 last edited by
                #7

                Thanks to all for your responses.
                I did a mix of your answers (using more the answer of @J.Hilk) and it works perfectly.

                1 Reply Last reply
                0

                1/7

                11 Sept 2018, 13:59

                • Login

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