Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get dialog red 'x' close button signal in QML

Tags:

dialog

qt

qml

How can I intercept the signal of the red 'x' close button of a dialog in qml?

Dialog
{
    id : dialog1
    visible  : false
    title : "dialog1"

    onRejected:
    {
        console.log("Red button x clicked signal")  // Not working
    }
    Button
    {
        id: exitButton
        text : "Exit"
        onClicked : 
        {
           console.log("exit button clicked") // this works
           dialog1.visible = false
        }
    }

I've tried all signals of qml dialog, and none seem to work for the x red button.

like image 672
asdfasdf Avatar asked Nov 08 '22 03:11

asdfasdf


1 Answers

Here I want the "X" button to behave exactly like it being rejected. You could call a different signal if you wanted, but I personally kept it to be the same as the rejected signal.

signal yesButtonClicked()
signal noButtonClicked()
signal rejectedButtonClicked()
signal acceptedButtonClicked()
property bool xButton: true

Dialog{

    id: dialogId
    title: dialogTitle
    onYes: {
        xButton = false
        yesButtonClicked()
    }
    onNo: {
        xButton = false
        noButtonClicked()
    }
    onRejected: {
        xButton = false
        rejectedButtonClicked()
    }
    onAccepted: {
        xButton = false
        acceptedButtonClicked()
    }
    onVisibilityChanged: {
        if (!this.visible && xButton){
            rejectedButtonClicked()
        }
        if (this.visible){
            xButton = true
        }
    }
like image 74
rchen Avatar answered Nov 15 '22 07:11

rchen