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:
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_())
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_())
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_())
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)
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With