[SOLVED] writing function for onClicked signal of nested button
-
--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:300MessageBox.YesNo{
captionText: "Are you sure you want to quit?"
///buttonYes.onClicked...
////buttonNo.onClicked...
}
@Then here is YesNo.qml
@
Rectangle{
width: parent.width
height: 200property string captionText: qsTr("")
RowLayout{
Button{
id: buttonYes
onClicked: parent.destroy()
}
Button{
id: buttonNo
onClicked: parent.destroy()
}
}
@Thanks in advance
-
Hi,
You can do it this way
YesNo.qml :
@Rectangle{
id: root
width: parent.width
height: 200signal yesClicked
signal noClickedproperty 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:300MessageBox.YesNo{
captionText: "Are you sure you want to quit?"
onYesClicked:
onNoClicked:}@
-
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/}
}@ -
Thank you to you both, that was exactly what I needed. I tried using a signal but clearly didn't do it correctly.
-
Also note that signals can have arguments inside them which can potentially be very useful, e.g. for passing strings or numbers etc.