问题
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.
回答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:
Using QQuickStyle in C++:
- add
QT += quickcontrols2
in your .pro and use#include <QQuickStyle>
andQQuickStyle::setStyle("Material");
in main.cpp
- add
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
.
- You can run from the console/CMD by adding the argument:
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.
- You can run from the console/CMD:
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