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. [SOLVED] writing function for onClicked signal of nested button
Qt 6.11 is out! See what's new in the release blog

[SOLVED] writing function for onClicked signal of nested button

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 2.2k 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.
  • G Offline
    G Offline
    GarethWalters
    wrote on last edited by
    #1

    --Hi everyone,

    I am trying to build a message box that can be simply added and then apply a custom function to the onClicked function of each button. I have manage to create properties that can be passed through, but I was wondering if I can do this with signals in QML only?

    @Main.qml

    import "../MessageBox" as MessageBox

    Rectangle{
    width :300
    height:300

    MessageBox.YesNo{
    captionText: "Are you sure you want to quit?"
    ///buttonYes.onClicked...
    ////buttonNo.onClicked...
    }
    @

    Then here is YesNo.qml
    @
    Rectangle{
    width: parent.width
    height: 200

    property string captionText: qsTr("")

    RowLayout{
    Button{
    id: buttonYes
    onClicked: parent.destroy()
    }
    Button{
    id: buttonNo
    onClicked: parent.destroy()
    }
    }
    @

    Thanks in advance

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MaxL
      wrote on last edited by
      #2

      Hi,

      You can do it this way

      YesNo.qml :

      @Rectangle{
      id: root
      width: parent.width
      height: 200

      signal yesClicked
      signal noClicked

      property string captionText: qsTr("")

      RowLayout{
      Button{
      id: buttonYes
      onClicked:
      {
      root.yesClicked()
      parent.destroy()
      }
      Button{
      id: buttonNo
      onClicked:
      {
      root.noClicked()
      parent.destroy()
      }
      }
      }@

      Then you can connect your signal in C++ if you manage to retrieve that MessageBox or in qml like this

      Main.qml

      @import "../MessageBox" as MessageBox

      Rectangle{
      width :300
      height:300

      MessageBox.YesNo{
      captionText: "Are you sure you want to quit?"
      onYesClicked:
      onNoClicked:

      }@

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lucijan
        wrote on last edited by
        #3

        Edit: MaxL beat me to it...

        Yes, you can do it with signals, and you probably don't want to destroy the MessageBox. Rather use the visible and enabled properties of the MessageBox to hide/unhide the dialog.

        So it would look something like this:

        @Rectangle {
        //...
        signal yesClick()
        signal noClick()

        RowLayout{
            Button{
                 id: buttonYes
                onClicked: yesClick()
            }
            Button{
                id: buttonNo
                onClicked: noClick()
            }
        }
        

        }@

        Then in your main.qml you can handle the signals using something like:

        @MessageBox.YesNo{
        captionText: "Are you sure you want to quit?"
        onYesClicked: {/do something/}
        onNoClicked: {/do something else/}
        }@

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GarethWalters
          wrote on last edited by
          #4

          Thank you to you both, that was exactly what I needed. I tried using a signal but clearly didn't do it correctly.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lucijan
            wrote on last edited by
            #5

            Also note that signals can have arguments inside them which can potentially be very useful, e.g. for passing strings or numbers etc.

            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