在 qml Qt 中设置样式

6
我想为我的qml元素设置样式。为此,我想使用像Material Style这样的样式。使用可以在以下位置找到的示例:https://doc.qt.io/qt-5/qtquickcontrols2-material.html
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12

ApplicationWindow {
   visible: true

   Material.theme: Material.Dark
   Material.accent: Material.Purple

   Column {
       anchors.centerIn: parent

       RadioButton { text: qsTr("Small") }
       RadioButton { text: qsTr("Medium");  checked: true }
       RadioButton { text: qsTr("Large") }
     }
}

给我看到我附上的图片中的结果。 无论我使用哪种样式,都没有任何变化。
我目前正在使用Windows 10操作系统下的最新免费Qt版本。
有人能帮帮我吗? 在QML 中是否可以全局覆盖一个样式并制作自己的样式?
2个回答

12
根据文档所述:

要使用 Material 样式运行应用程序,请参见在 Qt Quick Controls 中使用样式

在 Qt Quick Controls 2 中有几种设置样式的方法:
  1. 在 C++ 中使用 QQuickStyle

    • 在 .pro 文件中添加 QT += quickcontrols2,并在 main.cpp 中使用 #include <QQuickStyle>QQuickStyle::setStyle("Material");
  2. 命令行参数

    • 你可以通过在控制台/CMD 中添加参数运行: ./your_executable -style material
    • 如果您使用 Qt Creator,可以进入“项目”->“构建和运行”->“运行”,在“命令行参数”中添加:-style material

enter image description here

  1. Environment variable:

    • You can run from the console/CMD: QT_QUICK_CONTROLS_STYLE=material ./your_executable
    • If you are using Qt Creator you can add it in the section Projects-> Build & Run-> Run-> Run Environment.

    enter image description here

    • or add qputenv("QT_QUICK_CONTROLS_STYLE", "material"); in main.cpp.
  2. Configuration file:

    The qtquickcontrols2.conf file must be created:

    [Controls]
    Style=Material
    

    and must be in a qresource:

    <RCC>
        <qresource prefix="/">
            <file>main.qml</file>
            <file>qtquickcontrols2.conf</file>
        </qresource>
    </RCC>
    

问题出在我编辑了错误的项目。非常感谢!有时候问题就存在于自己的思维定势中。 :D - Multsens

1
你需要在C++中设置样式。请参考Qt文档。因此,在你的主程序中添加QQuickStyle::setStyle("Material");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接