PyQt QML Material Design 按钮背景不会改变
问题描述
我是 QML 新手,我正在尝试更改按钮的背景颜色,但似乎没有任何效果.这是python代码:import sys
从 PyQt5.QtWidgets 导入 QApplication, QMainWindow从 PyQt5.QtCore 导入 QUrl从 PyQt5.QtQuick 导入 QQuickView如果 __name__ == "__main__":应用程序 = QApplication(sys.argv)视图 = QQuickView()view.setSource(QUrl('basic.qml'))视图.show()sys.exit(app.exec_())
还有basic.qml:
导入QtQuick 2.0导入 QtQuick.Controls 2.0导入 QtQuick.Controls.Material 2.0导入 QtQuick.Controls.Material 2.3长方形 {宽度:600;高度:150;颜色:Material.color(Material.Red)按钮 {文本:qsTr(按钮")突出显示:真材质.背景:材质.蓝绿色}}
这是它的外观,按钮不是Material.Teal",无论我尝试什么颜色,它仍然不起作用.我已经尝试过使用窗格和其他元素,但仍然没有.
这是我得到代码的地方:
解决方案你的情况有两种可能的解决方案:
<小时>- 如果您想使用 Material.background,您必须确定您要使用的样式是 Material,为此您可以选择以下选项之一:
选项1:通过sys.argv
添加样式:
导入系统从 PyQt5.QtGui 导入 QGuiApplication从 PyQt5.QtCore 导入 QUrl从 PyQt5.QtQuick 导入 QQuickView如果 __name__ == "__main__":sys.argv += ['--style', 'material']应用程序 = QGuiApplication(sys.argv)视图 = QQuickView()view.setSource(QUrl('basic.qml'))视图.show()sys.exit(app.exec_())
选项2:通过os.environ()
添加样式:
导入操作系统导入系统从 PyQt5.QtGui 导入 QGuiApplication从 PyQt5.QtCore 导入 QUrl从 PyQt5.QtQuick 导入 QQuickView如果 __name__ == "__main__":应用程序 = QGuiApplication(sys.argv)os.environ["QT_QUICK_CONTROLS_STYLE"] = "材料"视图 = QQuickView()view.setSource(QUrl('basic.qml'))视图.show()sys.exit(app.exec_())
选项 3:创建一个 qtquickcontrols2.conf 文件并使用 qresource 将其加载到应用程序中:
qtquickcontrols2.conf
<代码>;可以编辑此文件以更改应用程序的样式;有关详细信息,请参阅文档中的样式化 Qt Quick Controls 2:;http://doc.qt.io/qt-5/qtquickcontrols2-styles.html[控制]样式=材质
然后创建一个resource.qrc:
resource.qrc:
<qresource 前缀="/"><文件>qtquickcontrols2.conf</文件></qresource></RCC>
然后你必须将.qrc转换为.py:
pyrcc5 resource.qrc -o resource_rc.py
最后将resource_rc.py文件导入到main.py中
main.py:
从 PyQt5.QtGui 导入 QGuiApplication从 PyQt5.QtCore 导入 QUrl从 PyQt5.QtQuick 导入 QQuickView导入系统导入资源_rc如果 __name__ == "__main__":应用程序 = QGuiApplication(sys.argv)视图 = QQuickView()view.setSource(QUrl('basic.qml'))视图.show()sys.exit(app.exec_())
<小时>
- 或者不使用 Material 风格,只通过调色板来使用它们的颜色:
<小时>
导入QtQuick 2.0导入 QtQuick.Controls 2.3导入 QtQuick.Controls.Material 2.3长方形 {宽度:600;高度:150;颜色:Material.color(Material.Red)按钮 {文本:qsTr(按钮")突出显示:真调色板.dark:Material.color(Material.Teal)}}
I'm new to QML and I'm trying to change the background color of a button but nothing seems to work. Here's the python code: 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_())
And the 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
}
}
Here is how it looks, the button isn't 'Material.Teal' and no matter what color I try it still doesn't work. I've tried it with Pane and other elements but still nothing.
Here's where I got the code: http://doc.qt.io/qt-5/qtquickcontrols2-material.html#material-primary-attached-prop
I've tried it with other styles/methods like pallete (orFusion): 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")
}
}
}
解决方案 In your case there are 2 possible solutions:
- If you want to use Material.background you must establish that the style you are going to use is Material, and to do so you can choose one of the following options:
option 1: add the style through 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_())
option 2: add the style through 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_())
option 3: create a qtquickcontrols2.conf file and load it into the application using a 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
then a resource.qrc is created:
resource.qrc:
<RCC>
<qresource prefix="/">
<file>qtquickcontrols2.conf</file>
</qresource>
</RCC>
Then you must convert the .qrc to .py:
pyrcc5 resource.qrc -o resource_rc.py
and at the end the resource_rc.py file is imported into the 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_())
- or not use the Material style, and only use their colors through the use of palette:
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)
}
}
相关文章