gpt4 book ai didi

qt - 在 qml Qt 中设置样式

转载 作者:行者123 更新时间:2023-12-04 16:41:22 32 4
gpt4 key购买 nike

我想在 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 中全局覆盖样式并制作自己的样式。

enter image description here

最佳答案

作为the docs指出:

To run an application with the Material style, see Using Styles in Qt Quick Controls.

在Qt Quick Controls 2中有几种设置样式的方法:

  1. Using QQuickStyle in C++ :

    • 添加QT += quickcontrols2在你的 .pro 中使用 #include <QQuickStyle>QQuickStyle::setStyle("Material");在 main.cpp 中
  2. Command line argument :

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

enter image description here

  1. Environment variable :

    • 您可以从控制台/CMD 运行:QT_QUICK_CONTROLS_STYLE=material ./your_executable
    • 如果您使用的是 Qt Creator,则可以将其添加到“项目”->“构建和运行”->“运行”->“运行环境”部分。

    enter image description here

    • 或添加qputenv("QT_QUICK_CONTROLS_STYLE", "material");在 main.cpp 中。
  2. Configuration file :

    必须创建qtquickcontrols2.conf文件:

    [Controls]
    Style=Material

    并且必须在 qresource 中:

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

关于qt - 在 qml Qt 中设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60221572/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com