QML:应用 Material 主题的 ComboBox

huangapple go评论87阅读模式
英文:

QML: ComboBox applying Material them

问题

I'm not able to set the dark material theme on combobox menù meanwhile it works on the combobox itself.

Some suggestion?

n.b I'm working on a larger project with QT Design Studio 4.2

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtMultimedia 5.15
import VenusView_UI 1.0
import QtGraphicalEffects 1.15
import QtQuick.Controls.Material 2.15
import QtQuick.Controls.Styles 1.4

Rectangle {
    id: rectangle
    width: Constants.width
    height: Constants.height
    color: "#2a2929"
    border.width: 4
    property bool rec_clicked: true
    Material.theme: Material.Dark
    Material.accent: Material.Indigo
    ComboBox {
        id: comboBox
        x: 40
        y: 425
        displayText: "Resolution"
        model: ListModel {
            ListElement {
                text: "4K"
            }
            ListElement {
                text: "Full-HD"
            }
            ListElement {
                text: "HD-Ready"
            }
        }

        onActivated: {
            comboBox.displayText = comboBox.currentText
            console.log("Elemento selezionato: " + comboBox.currentText)
        }
    }
}

QML:应用 Material 主题的 ComboBox
QML:应用 Material 主题的 ComboBox
QML:应用 Material 主题的 ComboBox

英文:

I'm not able to set the dark material theme on combobox menù meanwhile it works on the combobox itself.

Some suggestion?

n.b I'm working on a larger project with QT Design Studio 4.2

QML:应用 Material 主题的 ComboBox
QML:应用 Material 主题的 ComboBox

QML:应用 Material 主题的 ComboBox

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtMultimedia 5.15
import VenusView_UI 1.0
import QtGraphicalEffects 1.15
import QtQuick.Controls.Material 2.15
import QtQuick.Controls.Styles 1.4

Rectangle {
    id: rectangle
    width: Constants.width
    height: Constants.height
    color: "#2a2929"
    border.width: 4
    property bool rec_clicked: true
    Material.theme: Material.Dark
    Material.accent: Material.Indigo
    ComboBox {
            id: comboBox
            x: 40
            y: 425
            displayText: "Resolution"
            model: ListModel{
                ListElement{
                    text:"4K"
                }
                ListElement{
                    text:"Full-HD"
                }
                ListElement{
                    text:"HD-Ready"
                }
            }

            onActivated: {
                        comboBox.displayText = comboBox.currentText
                        console.log("Elemento selezionato: " + comboBox.currentText)
                    }
        }
}

答案1

得分: 1

这可能是一个bug,因为文档中提到:

> 所有属性都可以针对任何窗口或项目进行指定,并且它们会自动传播到子项,就像字体一样。

当这些属性没有在WindowApplicationWindow上设置时,似乎传播到子项并不起作用。
这不仅仅是Qt Design Studio的问题,因为在Qt Creator中运行应用程序时,我也遇到了相同的错误行为。在您的情况下,解决方案是将这两行代码和必要的导入放入App.qml中。

Material.theme: Material.Dark
Material.accent: Material.Indigo
英文:

This might be a bug, because the documentation states

> All attributes can be specified for any window or item, and they automatically propagate to children in the same manner as fonts.

It seems like the propagation to the children isn't working when those properties aren't set on Window or ApplicationWindow.
This isn't a Qt Design Studio only issue, because I get the same faulty behavior when running the application in Qt Creator as well. The solution in your case would be to put those two lines and the necessary import into App.qml.

Material.theme: Material.Dark
Material.accent: Material.Indigo

答案2

得分: 1

Alternatively to setting the Material theme in code, you can also consider setting it via the special qtquickcontrols2.conf and adding it to your application's resources.

[Controls]
Style=Material
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
    <file>qtquickcontrols2.conf</file>
</qresource>
</RCC>

https://doc.qt.io/qt-5/qtquickcontrols2-configuration.html

英文:

Alternatively to setting the Material theme in code, you can also consider setting it via the special qtquickcontrols2.conf and adding it to your application's resources.

[Controls]
Style=Material
&lt;!DOCTYPE RCC&gt;&lt;RCC version=&quot;1.0&quot;&gt;
&lt;qresource prefix=&quot;/&quot;&gt;
    &lt;file&gt;qtquickcontrols2.conf&lt;/file&gt;
&lt;/qresource&gt;
&lt;/RCC&gt;

https://doc.qt.io/qt-5/qtquickcontrols2-configuration.html

huangapple
  • 本文由 发表于 2023年7月17日 23:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定