Setting the Style in qml Qt

痴心易碎 提交于 2021-02-18 18:12:15

问题


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.

enter image description here


回答1:


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>
    



回答2:


You have to set the style from C++ as well. See this Qt documentation.

So in you main add QQuickStyle::setStyle("Material");



来源:https://stackoverflow.com/questions/60221572/setting-the-style-in-qml-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!