在 qml Qt 中设置样式 [英] Setting the Style in qml Qt

查看:63
本文介绍了在 qml Qt 中设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 qml 中为我的元素设置样式.为此,我想使用像 Material Style 这样的样式.使用可以在以下位置找到的示例:

指出:

<块引用>

要运行具有 Material 样式的应用程序,请参阅

    • 或在 main.cpp 中添加 qputenv("QT_QUICK_CONTROLS_STYLE", "material");.
  1. 配置文件:

    必须创建 qtquickcontrols2.conf 文件:

    [控制]样式=材料

    并且必须在 qresource 中:

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

I want to set the Style for my elements in qml. For that, I want to use a style like Material Style. Using the example which can be found under:

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") }
     }
}

Gives me the result seen in the image I attached. No matter which Style I use, nothing changes.

I am currently using the newest free Qt version under a Windows 10 Os.

Can anyone help me? And is it possible to globally overwrite a Style and make an own Style, simply in QML.

解决方案

As the docs points out:

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

There are several ways to set the style in Qt Quick Controls 2:

  1. Using QQuickStyle in C++:

    • add QT += quickcontrols2 in your .pro and use #include <QQuickStyle> and QQuickStyle::setStyle("Material"); in main.cpp
  2. Command line argument:

    • You can run from the console/CMD by adding the argument: ./your_executable -style material.
    • If you use Qt Creator you can go to Projects-> Build & Run-> Run and in Command line arguments add: -style material.

  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.

    • 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>
    

这篇关于在 qml Qt 中设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆