简体   繁体   English

如何在Qt代码中访问Qt Build System项目(QBS)变量

[英]How to access Qt Build System project (QBS) variables in Qt code

Good day. 美好的一天。

I need to access specific QBS variables inside my Qt code. 我需要在Qt代码中访问特定的QBS变量。

An example is the Application's name, organisation or even the flavour, all variables specified like this in my application qbs file. 一个例子是应用程序的名称,组织甚至风格,所有变量都在我的应用程序qbs文件中指定。

import qbs

Project {

    // These variables should be available in C++ code.
    name: "my_app_name"
    organization: "Organisation_Name"
    flavour:"AppFlavour"
    minimumQbsVersion: "1.7.1"

    CppApplication {
        files: [
        ]
        Depends { name: "Qt"; submodules: ['core', 'network'] }

        cpp.cxxLanguageVersion: "c++11"

        cpp.defines: [
            "QT_DEPRECATED_WARNINGS",
        ]

        consoleApplication: true

        Group {
            name: "source"
            files: [
                "qconfigurationmanager.cpp",
            ]
        }

        Group {
            name: "header"
            files: [
                "qconfigurationmanager.h",
            ]
        }

        Group {     // Properties for the produced executable
            fileTagsFilter: "application"
            qbs.install: true
        }
    }
}

Looking at the Qt documentation for QBS , I did not find any reference to using QBS variables in Qt code. 查看QBS的Qt文档,我没有找到任何在Qt代码中使用QBS变量的参考。

This is the only link of using QBS variables, but only within the QBS file 是使用QBS变量的唯一链接,但仅限于QBS文件中

I would like to do this: 我想这样做:

QString appflavour = Qbs.get("flavour")

How can I do this? 我怎样才能做到这一点?

A possible option is to use a DEFINES and obtain the data through a macro: 一个可能的选择是使用DEFINES并通过宏获取数据:

import qbs

Project {
    minimumQbsVersion: "1.7.1"

    property string name: "my_app_name"
    property string organization: "Organisation_Name"
    property string flavour:"AppFlavour"

    CppApplication {
        Depends { name: "Qt"; submodules: ['core', 'network']}
        cpp.cxxLanguageVersion: "c++11"
        consoleApplication: true

        cpp.defines: [
            "QT_DEPRECATED_WARNINGS",
            "name=" + project.name,
            "organization=" +  project.organization,
            "flavour=" + project.flavour
        ]
...

#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString appname = QUOTE(name);
    QString organization = QUOTE(organization);
    QString appflavour = QUOTE(flavour);

    qDebug()<< appname << organization << appflavour;

...

Output: 输出:

"my_app_name" "Organisation_Name" "AppFlavour"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM