Qt World Summit: Submit your Presentation

How to set the style (for instance Material) for QtQuick QML PySide2 app?

  • Hello,

    I am trying an example of use Material style: https://doc.qt.io/qt-5/qtquickcontrols2-material.html but it renders without colors nor Material style at all:


    I believe I should use QQuickStyle.setStyle method which is not available in PySide2? https://doc.qt.io/qt-5/qquickstyle.html

    My example code:

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import QtQuick.Controls.Material 2.12
    ApplicationWindow {
        visible: true
        Column {
            anchors.centerIn: parent
            RadioButton { text: qsTr("Small") }
            RadioButton { text: qsTr("Medium");  checked: true }
            RadioButton { text: qsTr("Large") }
    		Button {
    		    text: qsTr("Button")
    		    highlighted: true
    		    Material.background: Material.Red
    	    Pane {
    		    width: 120
    	    	height: 120
    	    	Material.elevation: 6	
    		    Label {
    	    	    text: qsTr("I'm a card!")
    	        	anchors.centerIn: parent
    from PySide2.QtWidgets import QApplication
    from PySide2.QtQuick import QQuickView
    from PySide2.QtCore import QUrl
    app = QApplication([])
    view = QQuickView()
    url = QUrl("view.qml")

    What is a correct way to set a style?

  • You need to pass --style material to the command line and handle that as an argument of your QApplication. Another option is to do this from the code itself using the approach from this example

    sys.argv += ['--style', 'material']
    app = QGuiApplication(sys.argv)

  • Thanks, it works!

Log in to reply