Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Constants in qml files

Constants in qml files

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 20.0k 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.
  • B Offline
    B Offline
    BKBK
    wrote on last edited by
    #1

    We are using QT creator because a vendor used it and therefore now we are. There are multiple places where the same constants are used over and over again. Having written C and C++ for a long time I want to get rid of those magic numbers and put in constants. So I added a file named constants.qml and populated it with things like:
    const int MAX_SCREEN_WIDTH = 480;
    because all the QML stuff looks quite a bit like C code. When I included that in another qml file with:
    include constants.qml
    I was not real surprised it did not work. From the QT documents web page searches for "constant" and for "keyword const" were not productive.
    What is the syntax to use for creating a QML file with a list of constants for all the other QML files to use? That way I can replace all the lines of:
    width: 480
    with
    width: MAX_SCREEN_WIDTH

    J.HilkJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      as far as i know there is not const in QML
      Javascript has it so maybe you can use that.
      https://forum.qt.io/topic/27367/defining-constants-for-use-in-qml

      1 Reply Last reply
      0
      • B BKBK

        We are using QT creator because a vendor used it and therefore now we are. There are multiple places where the same constants are used over and over again. Having written C and C++ for a long time I want to get rid of those magic numbers and put in constants. So I added a file named constants.qml and populated it with things like:
        const int MAX_SCREEN_WIDTH = 480;
        because all the QML stuff looks quite a bit like C code. When I included that in another qml file with:
        include constants.qml
        I was not real surprised it did not work. From the QT documents web page searches for "constant" and for "keyword const" were not productive.
        What is the syntax to use for creating a QML file with a list of constants for all the other QML files to use? That way I can replace all the lines of:
        width: 480
        with
        width: MAX_SCREEN_WIDTH

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @BKBK you can make your properties readonly

        readonly property <propertyType> <propertyName> : <initialValue>
        
        Read-only properties must be assigned a value on initialization. After a read-only property is initialized, it no longer possible to give it a value, whether from imperative code or otherwise.
        

        on the other hand, I believe one is supposed to do this in a cpp class.
        Here an quick and dirty example:

        //class.h

        class GlobalConsts : public QObject{
        
        public:
            Q_INVOKABLE inline const int &a(){return m_a;}
            Q_INVOKABLE inline const int &b(){return m_b;}
        
        private:
        const int m_a = 21;
        const int m_b = 42;
        }
        

        //main.cpp

        qmlRegisterType<GlobalConsts > ("MyConsts",1,0,"GlobalConsts");
        

        //qml usage

        import MyConsts 1.0
        
        MyConsts {
            id: constants
        }
        
        Rectangle{
             x:    constants.a()
             y:    constants.b()
        }
        

        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.

        A 1 Reply Last reply
        4
        • J.HilkJ J.Hilk

          @BKBK you can make your properties readonly

          readonly property <propertyType> <propertyName> : <initialValue>
          
          Read-only properties must be assigned a value on initialization. After a read-only property is initialized, it no longer possible to give it a value, whether from imperative code or otherwise.
          

          on the other hand, I believe one is supposed to do this in a cpp class.
          Here an quick and dirty example:

          //class.h

          class GlobalConsts : public QObject{
          
          public:
              Q_INVOKABLE inline const int &a(){return m_a;}
              Q_INVOKABLE inline const int &b(){return m_b;}
          
          private:
          const int m_a = 21;
          const int m_b = 42;
          }
          

          //main.cpp

          qmlRegisterType<GlobalConsts > ("MyConsts",1,0,"GlobalConsts");
          

          //qml usage

          import MyConsts 1.0
          
          MyConsts {
              id: constants
          }
          
          Rectangle{
               x:    constants.a()
               y:    constants.b()
          }
          
          A Offline
          A Offline
          ashikur
          wrote on last edited by
          #4

          @J.Hilk It's saying MyConsts is not a type :(

          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