Add constants declarations to QT



  • 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.



    • 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


  • @BKBK said in Add constants declarations to QT:

    Please differentiate between integer and unsigned integer.

    Actually, all numbers in JS are double



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



  • @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??!!



  • @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
    

  • Moderators

    @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 Champions 2018

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.