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 change the Button's background color? My qt version is 6.4.3
QtWS25 Last Chance

How to change the Button's background color? My qt version is 6.4.3

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 6 Posters 657 Views
  • 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.
  • X Offline
    X Offline
    xgj1
    wrote on last edited by
    #1

    I want to change the background color of the button, but the default style can not be overridden. How can I solve this problem

    import QtQuick
    import QtQuick.Controls
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Button {
                id: button
                text: qsTr("Button text")
    
                background: Rectangle {
                        color: parent.down ? "green" :
                                (parent.hovered ? "blue" : "red")
                }
        }
    }
    
    

    When I move the mouse over the button, the default styles override my custom styles

    Snipaste_2024-04-07_16-36-58.png

    AdithyaA JoeCFDJ 2 Replies Last reply
    0
    • X Offline
      X Offline
      xgj1
      wrote on last edited by
      #2

      I follow the official document to operate, is my custom button method wrong?

      GrecKoG mzimmersM MesrineM 3 Replies Last reply
      0
      • X xgj1

        I want to change the background color of the button, but the default style can not be overridden. How can I solve this problem

        import QtQuick
        import QtQuick.Controls
        
        Window {
            width: 640
            height: 480
            visible: true
            title: qsTr("Hello World")
        
            Button {
                    id: button
                    text: qsTr("Button text")
        
                    background: Rectangle {
                            color: parent.down ? "green" :
                                    (parent.hovered ? "blue" : "red")
                    }
            }
        }
        
        

        When I move the mouse over the button, the default styles override my custom styles

        Snipaste_2024-04-07_16-36-58.png

        AdithyaA Offline
        AdithyaA Offline
        Adithya
        wrote on last edited by
        #3

        @xgj1 You can create a button using rectangle and customize to whatever you want and use mousearea to make the rectangle interactive. check : https://doc.qt.io/qt-6/qml-qtquick-mousearea.html#details

        1 Reply Last reply
        0
        • X xgj1

          I follow the official document to operate, is my custom button method wrong?

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          @xgj1 Not all Qt Quick Controls Styles can be customized : https://doc.qt.io/qt-6/qtquickcontrols-customize.html#customization-reference

          My guess is that you are under Windows.
          The recommended way to use Qt Quick Controls now is not to use dynamic styling but to import a specific style.

          So no import QtQuick.Controls, instead use import QtQuick.Controls.Basic (or Fusion, Material, Universal)

          1 Reply Last reply
          1
          • X xgj1

            I follow the official document to operate, is my custom button method wrong?

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @xgj1 what @GrecKo said is probably what is happening to you. You might try adding something like this to your main.cpp:

            #include <QSysInfo>
            const QString platform = QSysInfo::productType();
            QString l_styleName;
            l_styleName = (platform == "android")
                                      ? "Material"
                                      : (platform == "windows")
                                            ? "Universal"
                                            : "Fusion";
            QQuickStyle::setStyle(l_styleName);
            
            1 Reply Last reply
            0
            • X xgj1

              I follow the official document to operate, is my custom button method wrong?

              MesrineM Offline
              MesrineM Offline
              Mesrine
              wrote on last edited by
              #6

              @xgj1

              In the 'official documentation' they do not use parent they use the component id.
              Try replacing parent with button. My thinking is that the parent is not well-defined for the Rectangle .

              GrecKoG 1 Reply Last reply
              0
              • MesrineM Mesrine

                @xgj1

                In the 'official documentation' they do not use parent they use the component id.
                Try replacing parent with button. My thinking is that the parent is not well-defined for the Rectangle .

                GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                @Mesrine No.

                MesrineM 1 Reply Last reply
                0
                • GrecKoG GrecKo

                  @Mesrine No.

                  MesrineM Offline
                  MesrineM Offline
                  Mesrine
                  wrote on last edited by
                  #8

                  @GrecKo You are right, the cause is the style he is using has something else on top of the background. So one should use a Basic,... style.
                  Good to know.
                  He could select the style also at runtime,
                  https://doc.qt.io/qt-6/qtquickcontrols-styles.html#using-styles-in-qt-quick-controls
                  But as you said, compile-time style selection is better for these custom components.
                  In any case, I do not think parent inside the Rectangle is good.

                  1 Reply Last reply
                  0
                  • X xgj1

                    I want to change the background color of the button, but the default style can not be overridden. How can I solve this problem

                    import QtQuick
                    import QtQuick.Controls
                    
                    Window {
                        width: 640
                        height: 480
                        visible: true
                        title: qsTr("Hello World")
                    
                        Button {
                                id: button
                                text: qsTr("Button text")
                    
                                background: Rectangle {
                                        color: parent.down ? "green" :
                                                (parent.hovered ? "blue" : "red")
                                }
                        }
                    }
                    
                    

                    When I move the mouse over the button, the default styles override my custom styles

                    Snipaste_2024-04-07_16-36-58.png

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @xgj1 I tested your code on Ubuntu 22.04 and got blue color when the mouse hovers over the button. Qt version is 6.7.

                    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