在 Qt Qml Controls 中,ApplicationWindow 在运行时缺少原生主题

2022-01-19 00:00:00 qt5 qt qml c++

Qml Controls 默认带有一个不错的原生主题.当我通过解释器将我的程序作为 qml 文件运行时,它看起来很棒,但是,一旦我将我的代码复制到 c++ 后端并构建它,它看起来完全没有主题并且非常平淡.此外,我没有启用任何类型的控制样式来使原生主题无效.

Qml Controls comes with a nice native-like theme by default. When I run my program as qml files through the interpreter, it looks great, however, once I copy my code over to c++ backend and build it, it looks completely unthemed and very bland. Also I did not enable any sort of Control Styles to void the native looking theme.

我唯一改变的是因为我的主 qml 文件中的根对象是一个 ApplicationWindow,我将 main.cpp 文件从加载 qmlviewer 更改为创建我自己的应用程序引擎.我在想这可能是问题,但我不确定.

The only thing that I changed was since my root object in my main qml file is a ApplicationWindow, I changed the main.cpp file from loading qmlviewer, to creating my own application engine. I was thinking that this might be the issue but i'm not sure.

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>

int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    engine.load(QUrl("src/qml/main.qml"));
    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);

    window->show();
    return app.exec();
}

推荐答案

文档

在这个例子中我们使用的是 QApplication 而不是 QGuiApplication.尽管您可以改用 QGuiApplication,但这样做会消除依赖于平台的样式.这是因为它依赖于小部件模块来提供原生外观.

We are using QApplication and not QGuiApplication in this example. Though you can use QGuiApplication instead, doing this will eliminate platform-dependent styling. This is because it is relying on the widget module to provide the native look and feel.

如果这对你没有帮助,我会感到非常惊讶.

If this will not help you I will be terribly suprised.

相关文章