Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to use qml code to change ToolBar background color (Qt Quick Controls 2.0)
Forum Update on Tuesday, May 27th 2025

How to use qml code to change ToolBar background color (Qt Quick Controls 2.0)

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qtquickqtquickcontrolsmaterial design
6 Posts 3 Posters 11.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Accelerated
    wrote on last edited by Accelerated
    #1

    Hi guys,

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    import QtQuick.Controls.Material 2.0
    
    ApplicationWindow {
        id: window
    
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Page{
            id: page
            anchors.fill: parent
            header: ToolBar{
                id: toolBar
                Material.foreground: "white"
                Material.background: "white"
                Material.accent: "white"
                Label{
                    id: label
                    anchors.centerIn: parent
                    font.family: "Source Han Sans Light"
                    font.pixelSize: 24
                    color: "#f0f0f0"
                    text: "Main"
                }
            }
        }
    }
    
    

    ( ⊙ o ⊙ )

    I looked the example code, it seems to create a file called qtquickcontrols2.conf,

    but i find it is not convenient..such as changing theme color...

    Could you guys please teach me how to fix it? (=・ω・=)

    Thanks a lot. :)

    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      You seem to be already altering the background and foreground colors, so I suppose your question is actually "how to run the app with the Material style"? You have several options. Besides the configuration file, you can use C++, a command line argument, or an environment variable. More details: http://doc.qt.io/qt-5/qtquickcontrols2-styles.html.

      A 1 Reply Last reply
      0
      • jpnurmiJ jpnurmi

        You seem to be already altering the background and foreground colors, so I suppose your question is actually "how to run the app with the Material style"? You have several options. Besides the configuration file, you can use C++, a command line argument, or an environment variable. More details: http://doc.qt.io/qt-5/qtquickcontrols2-styles.html.

        A Offline
        A Offline
        Accelerated
        wrote on last edited by
        #3

        @jpnurmi
        Hi,

        Thanks for your answering.

        I can use the Material Style in Qt Quick, but my question is:

        How to change ToolBar background color?

        If you are free, you can run my code.

        😊😊

        Julien BJ 1 Reply Last reply
        0
        • A Accelerated

          @jpnurmi
          Hi,

          Thanks for your answering.

          I can use the Material Style in Qt Quick, but my question is:

          How to change ToolBar background color?

          If you are free, you can run my code.

          😊😊

          Julien BJ Offline
          Julien BJ Offline
          Julien B
          wrote on last edited by Julien B
          #4

          HI @Accelerated,

          You can try to change the background property of the ToolBar (See Customizing ToolBar)

          import QtQuick 2.7
          import QtQuick.Controls 2.0
          import QtQuick.Layouts 1.0
          import QtQuick.Controls.Material 2.0
          
          ApplicationWindow {
              id: window
          
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              Page{
                  id: page
                  anchors.fill: parent
                  header: ToolBar{
                      id: toolBar
                      Material.foreground: "white"
                      Material.background: "white"
                      Material.accent: "white"
                      Label{
                          id: label
                          anchors.centerIn: parent
                          font.family: "Source Han Sans Light"
                          font.pixelSize: 24
                          color: "#f0f0f0"
                          text: "Main"
          
                      }
                      background: Rectangle {
                          anchors.fill: parent
                          color : "red"
                      }
                  }
              }
          }
          
          
          1 Reply Last reply
          1
          • jpnurmiJ Offline
            jpnurmiJ Offline
            jpnurmi
            wrote on last edited by
            #5

            The Material style uses the primary color for ToolBar:

            ToolBar {
                Material.primary: Material.Red
            }
            
            A 1 Reply Last reply
            3
            • jpnurmiJ jpnurmi

              The Material style uses the primary color for ToolBar:

              ToolBar {
                  Material.primary: Material.Red
              }
              
              A Offline
              A Offline
              Accelerated
              wrote on last edited by
              #6

              @jpnurmi
              Hi jpnurmi!

              Thank you for you reply!

              I have solved my problem.

              (^__^) ~

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved