如何在Qt应用程序中支持应用程序变体

huangapple go评论69阅读模式
英文:

How do I support app variants in a Qt application

问题

我一直在思考如何在Qt项目中正确支持应用程序变体。该项目使用QML和QMake。也许是因为我的搜索技能有限,但我没有找到关于如何在Qt项目中支持应用程序变体的最佳实践的具体信息。应用程序在不同变体之间保持一致,除了使用特定的变体图像和颜色等。因此,QML代码应该是通用的。

我目前考虑的是,在构建项目时添加一个CONFIG参数,该参数将包括一个特定于应用程序的.qrc文件,用于指定变体的特定资源。这在某种程度上可以工作,就像我有以下示例一样:

<RCC>
    <qresource prefix="/" >
        <file alias="icon">icons/icon.png</file>
    </qresource>
</RCC>

<RCC>
    <qresource prefix="/" >
        <file alias="icon">variant/icons/icon.png</file>
    </qresource>
</RCC>

请注意,别名是为了使两个PNG文件有一个通用的名称,可以像这样引用它:

"qrc:/icon"

然而,它将始终选择基本.qrc文件中的一个,只有在基本.qrc文件中缺少时才会选择变体文件中的一个。似乎无论变体.qrc是否最后包括都不重要。

无论如何,任何关于Qt是否有适当支持构建应用程序变体的方法的指针将不胜感激。例如,是否创建每个变体的项目是更好的方法?

英文:

I've been struggling a bit on how to properly support app variant in a Qt project. The project uses QML and QMake. Maybe my search skills are lacking but I have not been able to find any specifics on what best practices are on how to support app variants in a Qt project. The app will stay consistent across variants except for using specific variant images and colors etc. so the QML code should be generic.

What I currently have considered is adding a CONFIG argument when building the project that will include an app specific .qrc file specifying specific resources for the variant. This works somewhat in the sense that if I have the following

&lt;RCC&gt;
    &lt;qresource prefix=&quot;/&quot;&gt;
        &lt;file alias=&quot;icon&quot;&gt;icons/icon.png&lt;/file&gt;
    &lt;/qresource&gt;
&lt;/RCC&gt;


&lt;RCC&gt;
    &lt;qresource prefix=&quot;/&quot;&gt;
        &lt;file alias=&quot;icon&quot;&gt;variant/icons/icon.png&lt;/file&gt;
    &lt;/qresource&gt;
&lt;/RCC&gt;

Note the alias was to have a common name for both pngs so it can be referenced like this.

&quot;qrc:/icon&quot;

However, it will always pick the one in the base .qrc file and only the one in the variant file if missing from the base .qrc. It does not seem to matter if the variant .qrc is included last.

Anyway, any pointers to how (and if) Qt have proper support for building app variants will be much appreciated. For instance is creating a project for each variant a better approach?

答案1

得分: 0

QFileSelector 允许您根据语言、操作系统或其他选择器来使用不同版本的文件。您将不同的变体放在以“+”为前缀的单独目录中,如下所示:

    images/background.png
    images/+android/background.png

在此示例中,如果您的操作系统被检测为 android,则将选择 +android/background.png,但其他任何操作系统都将使用默认的 background.png。

如果使用 QML,可以使用 QQmlFileSelector。使用方式如下:

QQmlEngine engine;
QQmlFileSelector* selector = new QQmlFileSelector(&amp;engine);

由于选择器要求您替换整个文件,可能会存在重复代码的缺点。因此,理想情况下,您应该将变体代码减小到尽可能小的组件。例如,不要复制整个对话框,如果可以将标题提取为一个组件并只复制该部分,将会节省大量时间和麻烦。

英文:

QFileSelector allows you to use different versions of files based on things like language, OS, or whatever other selector you want to use. You place your variants in separate directories prefixed with '+', like this:

    images/background.png
    images/+android/background.png

In this example, if your OS is detected as android, it will select the +android/background.png, but any other OS will use the default background.png.

If using QML, there is QQmlFileSelector. Use it like this:

QQmlEngine engine;
QQmlFileSelector* selector = new QQmlFileSelector(&amp;engine);

Since selectors require you to replace entire files, there is the downside of potentially having a lot of duplicated code. So ideally you should reduce the variant code to as small a component as possible. For example, rather than duplicating an entire dialog, if you can pull out the header as a component and just duplicate that piece, you'll save a lot of time and headaches.

huangapple
  • 本文由 发表于 2023年3月7日 22:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定