Qt    D e v e l o p m e n t    E n v i r o n m e n t    (IDE)

  1. Qt References

    No. Link Short Description Language
    1 Lexicon/Qt

    Qt-Entry in Lexicon with links to pages about download, documentation, tutorials, frameworks and the open source project.

    German, English
    Tab. List of references about the Qt-IDE and its handling.
  2. Qt Download

    The usual way to proceed is to download from the Qt download page. Qt is available according to several flavors. My choice is Open Source - the developed software must conform to the (L)GPL, i.e. the GNU (Lesser) General Public License. LGPL 3 and GPL 3 are recommended (status 2021-06).

    Standard procedure
    The Qt online installer is an executable which downloads content and provides all Qt binary & sources packages and the latest Qt Creator. June 2021 the current version is Qt 6.1.1. that I downloaded today on my Linux Kubuntu 20.4 system.

    For those who want more
    For developers who want forefront features, the Qt development team offers a possibility to connect to the source i.e. build the Qt libraries from the actual Qt's source code, stored in a Git repository.

    I myself selected the standard online installation which includes following:

    • Qt libraries pre-built for my development platforms ( Linux, OSX, )
    • QtCreator (the IDE)
    • Reference Documentation and examples
    • Qt source codes (if I decide to build the Qt framework and tools).
    • Add-on components

    Worth mentioning is the fact that the Qt-package includes a maintenance functionality for updating, adding or suppressing modules. Presently, the option is available via the qt-account, that a developer must open before downloading anything.

  3. Qt Project File

    1. Overview

      All necessary informations are available on-line: Instead of rewriting every issue extensively, I will identify main issues, provide readers with short descriptions and links for further details. The building steps in short:

      1. A Qt project is described by a .pro (project) file, processed by a Qt specific build automation tool, named qmake.
      2. qmake basically generates a (file) Makefile, including specific building rules for Qt constructs, e.g. moc, uic.
      3. In turn, Makefile is processed by the make tool available on the system e.g.
        • on OSX /usr/bin/make
        • on Linux /usr/bin/make

      General references:

      • For an short illustration of the whole building process click here.
      • For a comprehensive explanation about project files click here.

    2. Content of a rudimentary project file

      QT += core
      QT -= gui

      TARGET = CL_UConversionTable
      CONFIG += console
      CONFIG -= app_bundle

      TEMPLATE = app

      SOURCES += main.cpp \
      converter_generic.cpp\
      converter_temperature.cpp\
      cl_calculator.cpp \
      global_UnitConversionTable.cpp
      # converter_time.cpp

      HEADERS += \
      converter_generic.h\
      converter_temperature.h\
      cl_calculator.h \
      global_UnitConversionTable.h
      # converter_time.h

      Project file associated with "proto-project" Unit Converter

      Status in March 2016: So far only temperature units can be converted. A time converter is currently being written, but not yet accounted for.

      The project file above is simple, since the application to build is just a small console application (that could become larger, as the number of physical entities supported will grow). qmake understands a rich syntax enabling a fine tuning of software builds involving many more units.

    3. Qmake Syntax

      1. Variables

        List of predefined variables

        qmake supports a number of standard variables associated with one or (mostly) several predefined values (called options) to fine-tune the build process. To populate a variable several operators may be used:

        operator

        Description

        equal

        Variable =Value
        All previous values associated with this variable are erased.

        add

        Variable+=Value
        The set of all values associated with this variable has increased by one value.

        remove

        Variable-=Value
        The set of all values associated with this variable has diminished by one value.

        Tab. qmake operators upon variables

        Operator equal should be used cautiously, because in many cases build variables are already - at least partially - populated (see ). An example of use (reinitialization) is shown in one user defined Replace-Function headersAndSources

        The table below shows commonly used variables:

        CONFIG

        As its name implies CONFIG is associated with a set of options that define main aspects of the entire build process: project configuration, compiler options.

        1. Is option release active, then Makefile will include instructions to produce a release version of the software (excluding debugging information).

        2. Is option console active, then the target is a console application.

        3. Option qt means that the target is a Qt application or library (requires Qt library and header files). It is defined by default.

          Option qt can be removed, if appropriate.
          Option qt can be fine-tuned by adding or removing (extension) modules in variable QT. By default, Qt modules core and and gui are included.

          Be aware to add (operator +=) or remove (operator -=) options / modules to QT, not to define QT from the scratch (operator =) - unless you know what you do. (For instance, a Qt application without the core module will not build).

        4. Option x11 indicates that the project is an X11 application.
          Option is not supported if the target uses Qt.

        DEFINES

        Qt considers the set of values associated with this variable as C-compiler preprocessor macros (DEFINES).

        HEADERS This variable lists the H files (headers) to include.
        INCLUDEPATH This variable specifies include directories to be searched at compiling time.

        win32:INCLUDEPATH += "C:/mylibs/extra headers"
        unix:INCLUDEPATH  += "/home/user/extra headers"
        LIBS

        This variable specifies a list of libraries to be linked into the project.

        # in case the platform is MS-Windows
        win32:LIBS += "C:/mylibs/extra libs/extra.lib"
        # in case the platform is Unix (path and file name are entered separately)
        unix:LIBS += "-L/home/user/extra libs" -lextra
        The examples above show how to link libraries

        SOURCES This variable lists the CPP files to include. If a backslash (\) ends a line, the list stretches to the next line.
        Tab. Lists of pre-defined variables governing the build process with qmake

        Evaluating (dereferencing, expanding) variables

        To get the value of a variable - whether predefined or not -, there are two operators (prefixes): $$, $. Here examples:

        # write value of _PRO_FILE_PWD_ to console.
        message ($$_PRO_FILE_PWD_)

        # Assign value of one variable to another.
        MY_DIRECTORY=$$_PRO_FILE_PWD_
        MY_TARGET=TARGET_$${_PRO_FILE_PWD_}

        # Both syntaxes $$NAME or $${NAME} are equivalent,
        # except for that the second one allows seamless chaining of values

        In order to better understand the build mechanism, it is helpful to evaluate predefined variables, especially CONFIG (Clearly a significant amount of CONFIG settings are performed behind the scene).

        Variables can be evaluated at 2 different moments within the build process:

        • Operator $$ evaluates at the time when qmake is run.
        • Operator $   evaluates at the time when the generated Makefile is run - which is only required in specific circumstances.

      2. Comments

        In project files comments begin with the # character. To use # literally, write the contents of variable LITERAL_HASH instead ($$LITERAL_HASH).

      3. Functions

        Built-in Functions process the contents of variables during build processing. Qt distinguishes between 2 kinds of built-in Functions depending of the type of their return value:

        1. Replace Functions manipulate strings, paths, user inputs and call external tools.

          cat(filename)

          returns the contents of filename

          dirname(file)

          returns the path in filename (name of directory in which the file is stored)

          enumerate_vars

          returns a list of all defined variable names

          find(var, substr),

          returns all values associated to var matching substr, where substr is a regular expression

          MY_VAR = one two three
          MY_RES = $$find(MY_VAR, t.*)
          # MY_RES = two three

          Questions:
          The detailed syntax of regular expressions in qmake remains unclear at this stage or must be tested.

          first(variablename),

          returns the first value associated to variablename (a variable can encompass a list of values)

          getenv(variablename),

          returns value(s) of the environment variable variablename which equates to expanding the variable name $$(variablename), except that it additionally tackles variables with parentheses in their name.

          join(variablename, glue, before, after)

          joins all values associated with variablename into a single one. It uses string glue as a link between values. Strings before and after strings can be prepended and appended to the result. All three string arguments are optional and default to the empty string.

          last(variablename),

          returns the last value associated to variablename - see first(varname)

          member(variablename, position)

          returns value number position in the list associated to variablename. Option position defaults to 0 (the first value stored in list) - see also first() and last().

          replace(string, old_string, new_string),

          MY_VAR = This is a variable
          MY_RES = $$replace(MY_VAR, is, at)
          # MY_RES = That at a variable

          Each detected instance of old_string is replaced by new_string

          sprintf(string, arguments...)

          enables to write/modify string with values of the passed arguments, identified in string as %1 till %9 (at most 9 arguments are supported).

          system(command)

          COMMAND_OUTPUT = $$system(uname -s)
          if contains(COMMAND_OUTPUT, Darwin ){
            message(This looks like OSX ($$COMMAND_OUTPUT).)
          }

          Tab. List of built-in functions in qmake (of type Replace Functions) (lexicographical order)

        2. Test Functions return a boolean value that can be tested in the conditional parts of scopes - according to syntax Test_Function(Arguments){}.

          CONFIG(config1 [, config2])

          tests options currently associated to CONFIG

          With one parameter config1, it checks whether the configuration is set (active).
          Sometimes conflicting configurations are set e.g. options release and debug. Only one of both is active: the one set the most recently. The Qt documentation qualifies it as the active config. Here comes the second parameter into play:

          CONFIG(debug, debug|release)
          returns true, if both options have been set, but debug is active (for more details see also discussion).

          It is worth noticing that the values stored in CONFIG are treated specially by qmake: Each of the possible values can be directly used as a condition for a scope.

          contains(variablename, value)

          returns true, if value belongs to the list associated with variablename

          defined(name [,type])

          returns true, if name is defined as variable, replace function or test function. Argument type restricts the spectrum to one single category: var, replace or test.

          equals(variablename,value)

          test whether variablename (exactly) equals to value.

          error(string)

          qmake displays string as an error message and exits.

          eval(string)

          evaluates the content of string according to qmake syntactical rules and returns true.

          exists(filename)

          exists(($QTDIR)/lib/$(LIB_PAT)){
          # library with a specific pattern has been detected
          }

          export(variablename)

          exports the current value of variablename from the local context of a function into the global context.

          files(pattern [,recursive=false])

          returns a list of files matching the pattern.

          include(filename)

          includes the content of filename into the project file at the place where the statement occurs. It may be used to nest project files under circumstances.

          message(string)

          Always succeeds, and displays string as a general message to the user.

          List of built-in functions in qmake (of type Test Functions) (lexicographical order)

        User Defined Functions

        The user can define own functions

        defineReplace(functionName){
          # function code
        }
        Syntax of user defined Replace Function in qmake

        defineReplace(headersAndSources) {

          variable = $$1
          names = $$eval($$variable)
          headers =
          sources =

          for(name, names) {

            header = $${name}.h
            exists($$header) {
              headers += $$header
            }
            source = $${name}.cpp
            exists($$source) {
              sources += $$source
            }
          }

          return($$headers $$sources)
        }
        Example of a user defined Replace Function, named headersAndSources)

        Rem.:

        1. $$1 represents the first (and in this case only) argument of the function.
          $$ARGS returns the list of all arguments of function.

        2. eval(string) is a Test Function.

      4. Control Flow

        There are a few underlying schemes to control the treatment flow:
        if-then-else Use a test Function to express one single condition and branch the flow depending on the boolean value returned.

        Code example of a single if-then-else

        test_function {
          # test_function is successful (true)
          message(test_condition is fulfilled!)
        } else {
          # test_function is not successful (false)
          message(test_condition is not fulfilled!)
        }

        • Beware of the syntax: The opening parenthesis of scope { must be written on the same line as the condition (test_function) and must terminate the (condition) line.

        • A condition can be negated using prefix !.
          Code example of a negated condition

          !test_function {
            # test_function is not successful (false)
            message(test_condition is not fulfilled!)
          }

        if-then-else

        qmake supports the conjunction of conditions.
        There are 2 elementary conjunction operators (that can be combined): AND (qmake speaks of nested scope), OR

        • AND is expressed by :
        • OR   is expressed by |

        The first example shown above (single if-then-else) can be considered as a nested scope (since message is also a test function that always returns true).

        Code example of a nested scope (AND-conditions)

        test_function:message(test_condition is fulfilled!){
          # do what must be done, when test_function is successful (true)
        }

        In case of a conjunction of conditions there is also the possibility to use following construct in order to enhance legibility.
        Code example of the if construct.

        if (conjunction_of_conditions) {
          # do something, if conjunction is true
        }

        loop for(iterate, list)

        Code example

        LIST = 1 2 3 for(a, LIST) {
        # Do, for each value a in LIST
          exists(file.$${a}){
          # Do file.1 or file.2 or file.3 exist?
          message(file file.$${a} exists!)
          }
        }

        • Instead of parentheses {}, the shorter way would be
          for(a,LIST):exists(file.$${a}):message(file file.$${a} exists!)

        Tab. How to flow the flow within the build process
      5. Project Templates

        Variable TEMPLATE defines the type of project to be built.
        If not declared qmake assumes that an application (option app) is to be built. Options are:

        Option Short description
        app

        Creates a Makefile for building an application (default value)

        lib Creates a Makefile for building libraries.
        subdirs

        Creates a Makefile for building targets in subdirectories, specified in variable SUBDIRS. Each specified subdirectory must contain its own project file.

        aux

        Creates a Makefile that does not build anything (e.g. because the project does not need to be built)

        Tab. List of options for variable TEMPLATE