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. Button with MouseArea property and animation
Forum Updated to NodeBB v4.3 + New Features

Button with MouseArea property and animation

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
16 Posts 4 Posters 1.5k Views 1 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.
  • I Offline
    I Offline
    Ivelin
    wrote on 14 Mar 2024, 18:07 last edited by
    #1

    Hello,

    I want to have a button and define a MouseArea property for it, but when I have MouseArea the default animation goes away. For refenrence:

    Button {
        text: 'something'
    }
    

    This has a normal animation and this doesn't:

    Button {
        text:: 'anything'
        MouseArea {
    
       }
    }
    

    How can I have MouseArea overriding the onPressed property and also have a normal animation ?

    S 1 Reply Last reply 15 Mar 2024, 06:31
    0
    • I Ivelin
      14 Mar 2024, 18:07

      Hello,

      I want to have a button and define a MouseArea property for it, but when I have MouseArea the default animation goes away. For refenrence:

      Button {
          text: 'something'
      }
      

      This has a normal animation and this doesn't:

      Button {
          text:: 'anything'
          MouseArea {
      
         }
      }
      

      How can I have MouseArea overriding the onPressed property and also have a normal animation ?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 15 Mar 2024, 06:31 last edited by
      #2

      @Ivelin you can make the MouseArea transparent by not accepting the mouse event (set accepted to false). Buuuuut.... why do you put a MouseArea in a Button?! It already does have a built-in mouse area.

      (Z(:^

      I 1 Reply Last reply 15 Mar 2024, 09:35
      0
      • R Offline
        R Offline
        Ronel_qtmaster
        wrote on 15 Mar 2024, 06:37 last edited by
        #3

        @Ivelin QML Button has already a mouseArea included.If for instance you wanted to create Your ownButton now you could add MouseArea to it.For exemple, if the button was a Rectangle

        Rectangle {
        id: customButton
        color: "blue"
        width:50
        height:30
        MouseArea {
        anchors.fill:parent
        }
        }
        something like this

        I 1 Reply Last reply 15 Mar 2024, 11:15
        0
        • S sierdzio
          15 Mar 2024, 06:31

          @Ivelin you can make the MouseArea transparent by not accepting the mouse event (set accepted to false). Buuuuut.... why do you put a MouseArea in a Button?! It already does have a built-in mouse area.

          I Offline
          I Offline
          Ivelin
          wrote on 15 Mar 2024, 09:35 last edited by
          #4

          @sierdzio, I want to have it so I can refer to onClicked and do stuff. Is it possible with the dafault animations ?

          B 1 Reply Last reply 15 Mar 2024, 11:08
          0
          • I Ivelin
            15 Mar 2024, 09:35

            @sierdzio, I want to have it so I can refer to onClicked and do stuff. Is it possible with the dafault animations ?

            B Offline
            B Offline
            Bob64
            wrote on 15 Mar 2024, 11:08 last edited by Bob64
            #5

            @Ivelin Button has a clicked signal, so it already supports an onClicked handler.

            These things you want to do with your MouseArea are already implemented by the button using its own MouseArea. It's also how it implements its "animations" (by which I presume you mean things like the changing background and border, etc.) when the mouse enters the button's area.

            When you added your own MouseArea it started to grab all of the mouse events and they stopped being propagated to the button's own MouseArea so all of the built-in functionality stopped working.

            1 Reply Last reply
            0
            • R Ronel_qtmaster
              15 Mar 2024, 06:37

              @Ivelin QML Button has already a mouseArea included.If for instance you wanted to create Your ownButton now you could add MouseArea to it.For exemple, if the button was a Rectangle

              Rectangle {
              id: customButton
              color: "blue"
              width:50
              height:30
              MouseArea {
              anchors.fill:parent
              }
              }
              something like this

              I Offline
              I Offline
              Ivelin
              wrote on 15 Mar 2024, 11:15 last edited by Ivelin
              #6

              @Ronel_qtmaster, is there some way to keep them ? Or maybe a way for me to implement them?

              B 1 Reply Last reply 15 Mar 2024, 12:22
              0
              • I Ivelin
                15 Mar 2024, 11:15

                @Ronel_qtmaster, is there some way to keep them ? Or maybe a way for me to implement them?

                B Offline
                B Offline
                Bob64
                wrote on 15 Mar 2024, 12:22 last edited by
                #7

                @Ivelin you are going to have to explain exactly what it is you are trying to do. I have already answered you about onClicked and you appear to have ignored what I said. Let me know if you did not understand.

                I 2 Replies Last reply 15 Mar 2024, 16:23
                1
                • B Bob64
                  15 Mar 2024, 12:22

                  @Ivelin you are going to have to explain exactly what it is you are trying to do. I have already answered you about onClicked and you appear to have ignored what I said. Let me know if you did not understand.

                  I Offline
                  I Offline
                  Ivelin
                  wrote on 15 Mar 2024, 16:23 last edited by Ivelin
                  #8

                  @Bob64, hello

                  Sorry for the misunderstanding

                  I made your answer marked as a solution, but I just realized that it is not working as I expected.

                  Let me try again.

                  I want to have a button and I want to do stuff on click, but when I use the onClicked proeprty that is built-in in the qml component it removes the default button animation. It removes it with MouseArea onClicked as well.

                  By animation I mean the effect that happens after clicking the button.

                  How can I have the onClicked property as well as the animation ?

                  Could you please clear my thoughts here?

                  B 1 Reply Last reply 15 Mar 2024, 16:57
                  0
                  • I Ivelin has marked this topic as solved on 15 Mar 2024, 16:23
                  • I Ivelin marked this topic as a regular topic on 15 Mar 2024, 16:30
                  • I Ivelin marked this topic as a question on 15 Mar 2024, 16:30
                  • I Ivelin has marked this topic as solved on 15 Mar 2024, 16:30
                  • I Ivelin has marked this topic as unsolved on 15 Mar 2024, 16:30
                  • B Bob64
                    15 Mar 2024, 12:22

                    @Ivelin you are going to have to explain exactly what it is you are trying to do. I have already answered you about onClicked and you appear to have ignored what I said. Let me know if you did not understand.

                    I Offline
                    I Offline
                    Ivelin
                    wrote on 15 Mar 2024, 16:45 last edited by
                    #9

                    @Bob64

                    In my current scenario I have a custom button:

                    Button {
                        id:control1
                      
                        implicitWidth: 46
                        implicitHeight: 46
                    
                        property real radius: 8
                        property color backgroundColor: "#14A44D"
                        property string setIcon: ""
                        property color textColor: "#FFFFFF"
                    
                        property real borderWidth: 0
                        property color borderColor: "transparent"
                        font.pixelSize: FontStyle.h3
                        font.family: FontStyle.getContentFont.name
                        font.bold: Font.Bold
                        font.weight: Font.Bold
                    
                        signal clicked
                    
                        MouseArea{
                            id:mouseArea
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            anchors.fill: parent
                    
                            onClicked: control1.clicked()
                    
                        }
                        onPressed: {
                            indicator.mx = mouseArea.mouseX
                            indicator.my = mouseArea.mouseY
                            main.restart()
                        }
                    }
                    

                    I already tried using the built-in qml property onClicked, but that way control1.clicked() doesn't get called. And the way I have done it there is no animation when I click the button. by animation I mean the effect after the button is clicked

                    Here is how I use it in my main qml

                            CustomButton {
                                anchors.horizontalCenter: parent.horizontalCenter
                                radius: 0
                                backgroundColor: "#00c1c9"
                                implicitHeight: 50
                                implicitWidth: 300
                                text: qsTr("Login")
                                onClicked: {
                                usernameField.focus = false;
                                passwordField.focus = false;
                                var jsonObject = {
                                    "username": usernameField.text,
                                    "password": passwordField.text
                                };
                    
                                    networkManager.post(jsonObject, "https://secureme.live/register")
                                    
                                }
                            }
                    

                    How can I have do my http request after onClick and still have the default animation when the button is clicked ?

                    Could you take a look please?

                    B 1 Reply Last reply 15 Mar 2024, 17:00
                    0
                    • I Ivelin
                      15 Mar 2024, 16:23

                      @Bob64, hello

                      Sorry for the misunderstanding

                      I made your answer marked as a solution, but I just realized that it is not working as I expected.

                      Let me try again.

                      I want to have a button and I want to do stuff on click, but when I use the onClicked proeprty that is built-in in the qml component it removes the default button animation. It removes it with MouseArea onClicked as well.

                      By animation I mean the effect that happens after clicking the button.

                      How can I have the onClicked property as well as the animation ?

                      Could you please clear my thoughts here?

                      B Offline
                      B Offline
                      Bob64
                      wrote on 15 Mar 2024, 16:57 last edited by
                      #10

                      @Ivelin that is not what I would expect.

                      What happens if you run this? (I am using Qt 5.15.10 BTW.)

                      import QtQuick 2.15
                      import QtQuick.Window 2.15
                      import QtQuick.Controls 2.15
                      
                      Window {
                          width: 640; height: 480
                          visible: true
                          Button {
                              width: 150;  height: 100
                              anchors.centerIn: parent
                              onClicked: console.log("HELLO")
                          }
                      }
                      

                      If you run with the default style, there isn't much animation anyway but there is at least a change of button colour on clicking. If I run with Material style, I see changes on entering the button area with the mouse, and various effects on clicking.

                      None of this seems to have been affected by adding my onClicked handler.

                      1 Reply Last reply
                      0
                      • I Ivelin
                        15 Mar 2024, 16:45

                        @Bob64

                        In my current scenario I have a custom button:

                        Button {
                            id:control1
                          
                            implicitWidth: 46
                            implicitHeight: 46
                        
                            property real radius: 8
                            property color backgroundColor: "#14A44D"
                            property string setIcon: ""
                            property color textColor: "#FFFFFF"
                        
                            property real borderWidth: 0
                            property color borderColor: "transparent"
                            font.pixelSize: FontStyle.h3
                            font.family: FontStyle.getContentFont.name
                            font.bold: Font.Bold
                            font.weight: Font.Bold
                        
                            signal clicked
                        
                            MouseArea{
                                id:mouseArea
                                hoverEnabled: true
                                cursorShape: Qt.PointingHandCursor
                                anchors.fill: parent
                        
                                onClicked: control1.clicked()
                        
                            }
                            onPressed: {
                                indicator.mx = mouseArea.mouseX
                                indicator.my = mouseArea.mouseY
                                main.restart()
                            }
                        }
                        

                        I already tried using the built-in qml property onClicked, but that way control1.clicked() doesn't get called. And the way I have done it there is no animation when I click the button. by animation I mean the effect after the button is clicked

                        Here is how I use it in my main qml

                                CustomButton {
                                    anchors.horizontalCenter: parent.horizontalCenter
                                    radius: 0
                                    backgroundColor: "#00c1c9"
                                    implicitHeight: 50
                                    implicitWidth: 300
                                    text: qsTr("Login")
                                    onClicked: {
                                    usernameField.focus = false;
                                    passwordField.focus = false;
                                    var jsonObject = {
                                        "username": usernameField.text,
                                        "password": passwordField.text
                                    };
                        
                                        networkManager.post(jsonObject, "https://secureme.live/register")
                                        
                                    }
                                }
                        

                        How can I have do my http request after onClick and still have the default animation when the button is clicked ?

                        Could you take a look please?

                        B Offline
                        B Offline
                        Bob64
                        wrote on 15 Mar 2024, 17:00 last edited by
                        #11

                        @Ivelin I sent my last post before seeing yours.

                        In your CustomButton you still have a MouseArea. As explained, this will override the built-in MouseArea of the Button and the behaviours you expect will no longer work.

                        I would just get rid of the MouseArea and add your onClicked handler directly to your CustomButton.

                        I 1 Reply Last reply 15 Mar 2024, 17:23
                        0
                        • B Bob64
                          15 Mar 2024, 17:00

                          @Ivelin I sent my last post before seeing yours.

                          In your CustomButton you still have a MouseArea. As explained, this will override the built-in MouseArea of the Button and the behaviours you expect will no longer work.

                          I would just get rid of the MouseArea and add your onClicked handler directly to your CustomButton.

                          I Offline
                          I Offline
                          Ivelin
                          wrote on 15 Mar 2024, 17:23 last edited by
                          #12

                          @Bob64, hello

                          your code works just as expected.

                          The problem is that when I do this:

                          Button {
                              id:control1
                            
                              implicitWidth: 46
                              implicitHeight: 46
                          
                              property real radius: 8
                              property color backgroundColor: "#14A44D"
                              property string setIcon: ""
                              property color textColor: "#FFFFFF"
                          
                              property real borderWidth: 0
                              property color borderColor: "transparent"
                              font.pixelSize: FontStyle.h3
                              font.family: FontStyle.getContentFont.name
                              font.bold: Font.Bold
                              font.weight: Font.Bold
                          
                              signal clicked
                          
                              onClicked: control1.clicked() 
                              
                              onPressed: {
                                  indicator.mx = mouseArea.mouseX
                                  indicator.my = mouseArea.mouseY
                                  main.restart()
                              }
                          }
                          

                          control1.clicked() doesn't get called. Why is that?

                          I have provided how I use it in my main qml upwards

                          B 1 Reply Last reply 15 Mar 2024, 17:25
                          0
                          • I Ivelin
                            15 Mar 2024, 17:23

                            @Bob64, hello

                            your code works just as expected.

                            The problem is that when I do this:

                            Button {
                                id:control1
                              
                                implicitWidth: 46
                                implicitHeight: 46
                            
                                property real radius: 8
                                property color backgroundColor: "#14A44D"
                                property string setIcon: ""
                                property color textColor: "#FFFFFF"
                            
                                property real borderWidth: 0
                                property color borderColor: "transparent"
                                font.pixelSize: FontStyle.h3
                                font.family: FontStyle.getContentFont.name
                                font.bold: Font.Bold
                                font.weight: Font.Bold
                            
                                signal clicked
                            
                                onClicked: control1.clicked() 
                                
                                onPressed: {
                                    indicator.mx = mouseArea.mouseX
                                    indicator.my = mouseArea.mouseY
                                    main.restart()
                                }
                            }
                            

                            control1.clicked() doesn't get called. Why is that?

                            I have provided how I use it in my main qml upwards

                            B Offline
                            B Offline
                            Bob64
                            wrote on 15 Mar 2024, 17:25 last edited by
                            #13

                            @Ivelin I don't know. Try getting rid of signal clicked. It might be masking the Button signal.

                            I 1 Reply Last reply 15 Mar 2024, 17:46
                            0
                            • B Bob64
                              15 Mar 2024, 17:25

                              @Ivelin I don't know. Try getting rid of signal clicked. It might be masking the Button signal.

                              I Offline
                              I Offline
                              Ivelin
                              wrote on 15 Mar 2024, 17:46 last edited by
                              #14

                              @Bob64, yup that was the problem.

                              There is one more thing though

                              Button {
                                  id:control
                              
                                  implicitWidth: 46
                                  implicitHeight: 46
                              
                                  property real radius: 8
                                  property color backgroundColor: "#14A44D"
                                  property string setIcon: ""
                                  property color textColor: "#FFFFFF"
                              
                                  property real borderWidth: 0
                                  property color borderColor: "transparent"
                                  font.pixelSize: FontStyle.h3
                                  font.family: FontStyle.getContentFont.name
                                  font.bold: Font.Bold
                                  font.weight: Font.Bold
                              
                              
                                  background: Rectangle{
                                      implicitHeight: control.implicitHeight
                                      implicitWidth: control.implicitWidth
                                      radius: control.radius
                                      color: control.backgroundColor
                                      border.width: control.borderWidth
                                      border.color: control.borderColor
                              
                              
                                  }
                              
                                  onClicked: {
                                     control1.clicked()
                                  }
                              }
                              

                              I am just trying to add a background and it works, but the animation goes away agian.. why is that ?

                              B R 2 Replies Last reply 18 Mar 2024, 09:55
                              0
                              • I Ivelin
                                15 Mar 2024, 17:46

                                @Bob64, yup that was the problem.

                                There is one more thing though

                                Button {
                                    id:control
                                
                                    implicitWidth: 46
                                    implicitHeight: 46
                                
                                    property real radius: 8
                                    property color backgroundColor: "#14A44D"
                                    property string setIcon: ""
                                    property color textColor: "#FFFFFF"
                                
                                    property real borderWidth: 0
                                    property color borderColor: "transparent"
                                    font.pixelSize: FontStyle.h3
                                    font.family: FontStyle.getContentFont.name
                                    font.bold: Font.Bold
                                    font.weight: Font.Bold
                                
                                
                                    background: Rectangle{
                                        implicitHeight: control.implicitHeight
                                        implicitWidth: control.implicitWidth
                                        radius: control.radius
                                        color: control.backgroundColor
                                        border.width: control.borderWidth
                                        border.color: control.borderColor
                                
                                
                                    }
                                
                                    onClicked: {
                                       control1.clicked()
                                    }
                                }
                                

                                I am just trying to add a background and it works, but the animation goes away agian.. why is that ?

                                B Offline
                                B Offline
                                Bob64
                                wrote on 18 Mar 2024, 09:55 last edited by
                                #15

                                @Ivelin it's because the animation is implemented in the background of the Button you are using - this is the implementation as provided by Qt. When you implement your own background you completely override the original implementation, so if you want the animations and you want your own customisation, you need to implement it all yourself.

                                It is a weakness of QML's customisation approach IMO that fine-grained customisations are not generally supported - it tends to be all or nothing as soon as you want any customisation.

                                What you could do is to find the Button implementation in the source for the style you are using and use that as the basis for your customised background.

                                1 Reply Last reply
                                0
                                • I Ivelin
                                  15 Mar 2024, 17:46

                                  @Bob64, yup that was the problem.

                                  There is one more thing though

                                  Button {
                                      id:control
                                  
                                      implicitWidth: 46
                                      implicitHeight: 46
                                  
                                      property real radius: 8
                                      property color backgroundColor: "#14A44D"
                                      property string setIcon: ""
                                      property color textColor: "#FFFFFF"
                                  
                                      property real borderWidth: 0
                                      property color borderColor: "transparent"
                                      font.pixelSize: FontStyle.h3
                                      font.family: FontStyle.getContentFont.name
                                      font.bold: Font.Bold
                                      font.weight: Font.Bold
                                  
                                  
                                      background: Rectangle{
                                          implicitHeight: control.implicitHeight
                                          implicitWidth: control.implicitWidth
                                          radius: control.radius
                                          color: control.backgroundColor
                                          border.width: control.borderWidth
                                          border.color: control.borderColor
                                  
                                  
                                      }
                                  
                                      onClicked: {
                                         control1.clicked()
                                      }
                                  }
                                  

                                  I am just trying to add a background and it works, but the animation goes away agian.. why is that ?

                                  R Offline
                                  R Offline
                                  Ronel_qtmaster
                                  wrote on 18 Mar 2024, 10:22 last edited by
                                  #16

                                  @Ivelin it is simple.When you customize a quick control, you have to hadle the change of color, size and other things yourself.I think you have to read the documentation about customizing quick controls https://doc.qt.io/qt-6/qtquickcontrols-customize.html

                                  1 Reply Last reply
                                  0

                                  1/16

                                  14 Mar 2024, 18:07

                                  • Login

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