PyQt QML Material 设计按钮背景不会改变
PyQt QML Material Design button background won't change
我是 QML 的新手,我正在尝试更改按钮的背景颜色,但似乎没有任何效果。
这是 python 代码:import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
app = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
和 basic.qml:
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Material 2.3
Rectangle {
width:600;height:150;
color: Material.color(Material.Red)
Button {
text: qsTr("Button")
highlighted: true
Material.background: Material.Teal
}
}
这是它的外观,按钮不是 'Material.Teal',无论我尝试什么颜色,它仍然不起作用。我已经尝试过使用 Pane 和其他元素,但仍然没有。
这是我获得代码的地方:http://doc.qt.io/qt-5/qtquickcontrols2-material.html#material-primary-attached-prop
我已经用其他 styles/methods 试过它,比如 palette(或 Fusion):
https://doc.qt.io/qt-5.10/qml-qtquick-controls2-control.html#palette-prop
Page {
palette.text: "red"
Column {
Label {
text: qsTr("This will use red color...")
}
Switch {
text: qsTr("... and so will this")
}
}
}
检查此部分的样式和颜色:
http://doc.qt.io/qt-5/qml-qtquick-controls-styles-buttonstyle.html
对于您的情况,有 2 种可能的解决方案:
- 如果您想使用 Material.background,您必须确定您要使用的样式是 Material,为此您可以选择以下选项之一:
选项1:通过sys.argv
添加样式:
import sys
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
sys.argv += ['--style', 'material']
app = QGuiApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
选项2:通过os.environ()
添加样式:
import os
import sys
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
选项 3:创建一个 qtquickcontrols2.conf 文件并使用 qresource 将其加载到应用程序中:
qtquickcontrols2.conf
; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
[Controls]
Style=Material
然后创建了一个resource.qrc:
resource.qrc:
<RCC>
<qresource prefix="/">
<file>qtquickcontrols2.conf</file>
</qresource>
</RCC>
然后你必须将.qrc 转换为.py:
pyrcc5 resource.qrc -o resource_rc.py
最后 resource_rc.py 文件被导入 main.py
main.py:
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
import sys
import resource_rc
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
- 或者不使用 Material 样式,只通过使用调色板来使用它们的颜色:
import QtQuick 2.0
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.3
Rectangle {
width:600;height:150;
color: Material.color(Material.Red)
Button {
text: qsTr("Button")
highlighted: true
palette.dark: Material.color(Material.Teal)
}
}
我是 QML 的新手,我正在尝试更改按钮的背景颜色,但似乎没有任何效果。 这是 python 代码:import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
app = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
和 basic.qml:
import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Material 2.3
Rectangle {
width:600;height:150;
color: Material.color(Material.Red)
Button {
text: qsTr("Button")
highlighted: true
Material.background: Material.Teal
}
}
这是它的外观,按钮不是 'Material.Teal',无论我尝试什么颜色,它仍然不起作用。我已经尝试过使用 Pane 和其他元素,但仍然没有。
这是我获得代码的地方:http://doc.qt.io/qt-5/qtquickcontrols2-material.html#material-primary-attached-prop
我已经用其他 styles/methods 试过它,比如 palette(或 Fusion): https://doc.qt.io/qt-5.10/qml-qtquick-controls2-control.html#palette-prop
Page {
palette.text: "red"
Column {
Label {
text: qsTr("This will use red color...")
}
Switch {
text: qsTr("... and so will this")
}
}
}
检查此部分的样式和颜色: http://doc.qt.io/qt-5/qml-qtquick-controls-styles-buttonstyle.html
对于您的情况,有 2 种可能的解决方案:
- 如果您想使用 Material.background,您必须确定您要使用的样式是 Material,为此您可以选择以下选项之一:
选项1:通过sys.argv
添加样式:
import sys
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
sys.argv += ['--style', 'material']
app = QGuiApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
选项2:通过os.environ()
添加样式:
import os
import sys
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
选项 3:创建一个 qtquickcontrols2.conf 文件并使用 qresource 将其加载到应用程序中:
qtquickcontrols2.conf
; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
[Controls]
Style=Material
然后创建了一个resource.qrc:
resource.qrc:
<RCC>
<qresource prefix="/">
<file>qtquickcontrols2.conf</file>
</qresource>
</RCC>
然后你必须将.qrc 转换为.py:
pyrcc5 resource.qrc -o resource_rc.py
最后 resource_rc.py 文件被导入 main.py
main.py:
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
import sys
import resource_rc
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(app.exec_())
- 或者不使用 Material 样式,只通过使用调色板来使用它们的颜色:
import QtQuick 2.0
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.3
Rectangle {
width:600;height:150;
color: Material.color(Material.Red)
Button {
text: qsTr("Button")
highlighted: true
palette.dark: Material.color(Material.Teal)
}
}