Project Properties

It would be nice if our project was configurable. Let's add some properties to our root project file:

Project {
    property string version: "1.0.0"
    property bool installDebugInformation: true
    property bool withTests: false
    property stringList autotestArguments: []
    property stringList autotestWrapper: []

    name: "My Project"
    minimumQbsVersion: "2.0"
    // ...

Now we can use those properties in our helper items and in products:

CppApplication {
    version: project.version
    consoleApplication: true
    install: true
    installDebugInformation: project.installDebugInformation
    // ...

Here, we access the project file using the special project value. If the nearest project in the project tree does not have the desired property, Qbs looks it up in the parent project, potentially all the way up to the top-level project. We also use the installDebugInformation property here. By default, it has the same value as install but we want to make the debug information install optional.

Let's now disable the tests if project.withTests is false:

SubProject {
    filePath: "test/test.qbs"
    Properties {
        condition: parent.withTests

Here we use the Properties item within the SubProject item. This item allows to override a subproject's properties which can be useful when adding some other Qbs project as a Git submodule. Of course, it is not very useful in our particular case since we only set the Project.condition property. We could achieve the same effect by setting the condition property of the SubProject item:

SubProject {
    filePath: "test/test.qbs"
    condition: parent.withTests

Another way would be to disable the test product. That way, an IDE would still show the whole project tree including disabled tests:

// qbs/imports/MyAutoTest.qbs
MyApplication {
    condition: project.withTests
    type: base.concat(["autotest"])

Let's finally make our AutotestRunner configurable too:

AutotestRunner {
    condition: parent.withTests
    arguments: parent.autotestArguments
    wrapper: parent.autotestWrapper

There are several ways to override project properties from the command line. First, the special project variable can be used to set the properties of the top-level project:

$ qbs resolve project.withTests:false

You can also refer to properties using project's name:

$ qbs resolve "projects.My Project.withTests:false"

This can again be useful for accessing the properties of external sub-projects. Note that since the project name contains spaces, we use quotes here.

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.