[solved] QML, Toolbar backgroud depending on WIndows or Android



  • Hi,
    i have a toolbar in my QML code like below. The style :ToolbarStyle should only valid for Android, not for Desktop. How can i do that?
    Thx

    toolBar: ToolBar {
        id: mainToolBar
        width: parent.width
        style: ToolBarStyle {
            background: Rectangle {
                width: parent.width
                height: parent.heigt
                color: "#fcdb00"
            }
        }
    
        RowLayout {
            anchors.fill: parent
            spacing: 0
            ToolButton { action: newsetupAction }
            ToolButton { action: opensetupAction }
            ToolBarSeparator {}
            ToolButton { action: savesetupAction }
            ToolButton { action: savesetupasAction }
            Item { Layout.fillWidth: true }
        }
    }

  • Moderators

    @HappyCoder Since style requires a Component you can encapsulate in ToolBarStyle and assign it to style. The you can add a condition for style to load Component as per platform. Eg:

    style: Qt.platform.os==="android" ? androidComponent : desktopComponent
    
    Component {
      id: androidComponent
      ToolBarStyle {
            background: Rectangle {
                width: parent.width
                height: parent.heigt
                color: "#fcdb00"
            }
        }
    }
    
    Component {
      id: desktopComponent
      ToolBarStyle {
            background: Rectangle {
                width: parent.width
                height: parent.heigt
                color: "red"
            }
        }
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.