Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. Qt Contribution
  4. Add constants declarations to QT

Add constants declarations to QT

Scheduled Pinned Locked Moved Solved Qt Contribution
8 Posts 6 Posters 4.8k 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 31 Jan 2018, 18:25 last edited by
    #1

    We, my particular project, have been introduced to QT because a development division of the Air Force uses it. I quickly noticed that hard coded values were repeated over and over in the code. The merits of using named constants is so great that there is no need to entertain that debate. I am quite surprised to find that QT does not provide any explicit method to declaring constants that can be used throughout any set of QML files. In my not so humble opinion this is a major oversight.
    Please add the ability to create a QML file of constants than can be used throughout any set of QML files with a simple “include” statement.
    Please make this an integral part of the QT environment, not some funky syntax kludged on to QT.
    Please make it as consistent with C and C++ as can be done. For example:
    const unsigned int MAX_SCREEN = 480;
    const float PI = 3.1415;
    const double PI_DOUBLE = 3.1415926535;
    const char[] = “Some String”;
    I would prefer to keep the C syntax of ending with a semicolon but that is not at all critical.
    Please differentiate between integer and unsigned integer. Don’t take the BASIC route and ignore that distinction. This may well be important for interfacing with C/C++ functions and classes.

    J 1 Reply Last reply 1 Feb 2018, 23:39
    0
    • K Offline
      K Offline
      Konstantin Tokarev
      wrote on 31 Jan 2018, 18:30 last edited by
      #2
      • This forum branch is dedicated to making contributions to Qt. Are you planning to implement these features yourself? If not, it's better to be discussed elsewhere, e.g. as feature requests at https://bugreports.qt.io or in mailing lists
      • QML uses JavaScript as its scripting language. This means that everything related to data types and syntax is mandated by JS standard and isn't subject to random customizations
      B 1 Reply Last reply 31 Jan 2018, 21:27
      1
      • K Offline
        K Offline
        Konstantin Tokarev
        wrote on 31 Jan 2018, 18:31 last edited by
        #3

        @BKBK said in Add constants declarations to QT:

        Please differentiate between integer and unsigned integer.

        Actually, all numbers in JS are double

        1 Reply Last reply
        2
        • P Offline
          P Offline
          Pablo J. Rogina
          wrote on 31 Jan 2018, 20:15 last edited by
          #4

          @BKBK you may want to consider taking a look at the Qt contribution guidelines

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • K Konstantin Tokarev
            31 Jan 2018, 18:30
            • This forum branch is dedicated to making contributions to Qt. Are you planning to implement these features yourself? If not, it's better to be discussed elsewhere, e.g. as feature requests at https://bugreports.qt.io or in mailing lists
            • QML uses JavaScript as its scripting language. This means that everything related to data types and syntax is mandated by JS standard and isn't subject to random customizations
            B Offline
            B Offline
            BKBK
            wrote on 31 Jan 2018, 21:27 last edited by
            #5

            @Konstantin-Tokarev I visited https://bugreports.qt.io but they won't or can't send a verification email to my government email address.
            As for contributing, I think that is something that should be an integral part of QT. I am a bit astounded that it is not already there. I probably don't have the chops to tackle this.
            Regarding the comment that everything in JS is a double. That is terrible. Worse that BASIC. Array access is via an index that is a double??!!

            J 1 Reply Last reply 1 Feb 2018, 07:15
            0
            • B BKBK
              31 Jan 2018, 21:27

              @Konstantin-Tokarev I visited https://bugreports.qt.io but they won't or can't send a verification email to my government email address.
              As for contributing, I think that is something that should be an integral part of QT. I am a bit astounded that it is not already there. I probably don't have the chops to tackle this.
              Regarding the comment that everything in JS is a double. That is terrible. Worse that BASIC. Array access is via an index that is a double??!!

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 1 Feb 2018, 07:15 last edited by
              #6

              @BKBK you can have something like that take a look at my poste in this thread

              ignore the readonly property part, the cpp part is what you're looking for

              if you don't want a cpp - QML object in each qml-file

              you can replace

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

              with

              GlobalConsts myConsts;
              engine.rootContext()->setContextProperty("constants", &myConsts);
              
              //Access in qml changes to
              property int abcd : constants.a()
              
              //or, if a is defined as Q_Property(int a READ a NOTIFY aChanged) 
              property int abcd : constants.a
              

              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
              • B BKBK
                31 Jan 2018, 18:25

                We, my particular project, have been introduced to QT because a development division of the Air Force uses it. I quickly noticed that hard coded values were repeated over and over in the code. The merits of using named constants is so great that there is no need to entertain that debate. I am quite surprised to find that QT does not provide any explicit method to declaring constants that can be used throughout any set of QML files. In my not so humble opinion this is a major oversight.
                Please add the ability to create a QML file of constants than can be used throughout any set of QML files with a simple “include” statement.
                Please make this an integral part of the QT environment, not some funky syntax kludged on to QT.
                Please make it as consistent with C and C++ as can be done. For example:
                const unsigned int MAX_SCREEN = 480;
                const float PI = 3.1415;
                const double PI_DOUBLE = 3.1415926535;
                const char[] = “Some String”;
                I would prefer to keep the C syntax of ending with a semicolon but that is not at all critical.
                Please differentiate between integer and unsigned integer. Don’t take the BASIC route and ignore that distinction. This may well be important for interfacing with C/C++ functions and classes.

                J Offline
                J Offline
                JKSH
                Moderators
                wrote on 1 Feb 2018, 23:39 last edited by
                #7

                @BKBK said in Add constants declarations to QT:

                Regarding the comment that everything in JS is a double. That is terrible. Worse that BASIC. Array access is via an index that is a double??!!

                Please realize that Qt is composed of different parts:

                • C++ handles numbers in the way you are already familiar with.
                • QML does differentiate between integers and floating point numbers.
                • JavaScript treats all numbers as 64-bit IEEE 754 (floating point). Nonetheless, most modern JavaScript engines (including QML's built-in JavaScript engine) are smart enough to track and identify integers internally, and use integer operations where appropriate.

                If you don't like JavaScript's behaviour, feel free to contact the ECMA standards committee: https://ecma-international.org/ecma-262/5.1/#sec-4.3.19 (Note: This is outside Qt)

                @BKBK said in Add constants declarations to QT:

                I quickly noticed that hard coded values were repeated over and over in the code. The merits of using named constants is so great that there is no need to entertain that debate.

                Agreed; hard-coded values are bad practice.

                Please add the ability to create a QML file of constants than can be used throughout any set of QML files with a simple “include” statement.

                This is currently possible via a .js file instead of a .qml file:

                // GlobalConstants.js
                .pragma library
                
                var baseWidth = 800;
                var baseHeight = 400;
                
                // xyz.qml
                import "GlobalConstants.js" as Globals
                
                Rectangle {
                    width: Globals.baseWidth;
                    height: Globals.baseHeight;
                }
                

                Please make it as consistent with C and C++ as can be done.

                QML is similar to CSS, JavaScript, and JSON. QML is not similar to C/C++.

                From the point of view of language design, I would prefer new QML features to be consistent with CSS, JavaScript, and JSON instead of C/C++.

                I would prefer to keep the C syntax of ending with a semicolon but that is not at all critical.

                This is already supported. You can end all your QML assignments and JavaScript statements with a semicolon.

                See my example code above.

                Please differentiate between integer and unsigned integer. Don’t take the BASIC route and ignore that distinction. This may well be important for interfacing with C/C++ functions and classes.

                Do you mean you want to export QML/JavaScript constants to C++?

                Note that Qt-aware C++ code is required to interface with QML code.

                I visited https://bugreports.qt.io but they won't or can't send a verification email to my government email address.

                As I mentioned in https://forum.qt.io/topic/86940/novice-where-is-key-defined, perhaps the military server blocks emails from untrusted sources? Or perhaps the email landed in your junk mail folder?

                In any case, you can use your existing home account.

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

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 2 Feb 2018, 07:11 last edited by
                  #8

                  For reference: The bugreport resulting from this thread is: https://bugreports.qt.io/browse/QTCREATORBUG-19710

                  @BKBK: it is good practice to add a link here if the discussion is moved to another place. That way, later visitors can follow it and find more insights.

                  Thanks.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  4

                  8/8

                  2 Feb 2018, 07:11

                  • Login

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