如何在QML中获取对话框红色“X”关闭按钮信号

5
我该如何在qml中拦截对话框红色“x”关闭按钮的信号?
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
        }
    }

我尝试了所有的QML对话框信号,但是似乎没有一个适用于红色X按钮。


你考虑过使用 onClosing 信号吗?https://doc-snapshots.qt.io/qt5-5.7/qml-qtquick-window-window.html#closing-signal - Vedanshu
这不是 QML 中对话框的信号:http://doc.qt.io/qt-5/qml-qtquick-dialogs-dialog.html#accepted-signal - asdfasdf
onVisibleChanged 信号怎么样? - DuKes0mE
当您单击红色关闭按钮时,它会使用Qt.quit调用关闭对话框而不发出rejected信号。因此,如果您想在关闭后执行某些清理操作,请在按钮中使用dialog1.close();,这将发出rejected信号。 - Vedanshu
这个链接可能会有帮助:http://katecpp.github.io/close-event-qtquick/。 - Vedanshu
显示剩余4条评论
2个回答

1

我希望这里的“X”按钮能像被拒绝一样行为。如果你想的话,可以调用不同的信号,但我个人认为最好与被拒绝的信号相同。

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
        }
    }

0
我正在Ubuntu上使用Qt 5.9.1,尝试检测在QML MessageDialog窗口中单击"x" /关闭按钮。文档确实指出onRejected是应该触发的事件-但它没有!所以我使用onVisibleChanged并检查它是否不可见。那样可以工作。
Item {
    signal dialogClosed()
    property string dialogTitle: ""
    property int     dialogIcon: 0
    property string  dialogText: ""
    property bool dialogVisible: false
    MessageDialog {
        id: messageDialog
        title   : dialogTitle
        icon    : dialogIcon
        text    : dialogText
        visible : dialogVisible
        modality: Qt.ApplicationModal
        onAccepted: dialogClosed()
        // onRejected: dialogClosed() // Doesn't work in when "x" is clicked as documented!
        onVisibleChanged: { if( !this.visible ) dialogClosed(); }
    }
}

应该使用 onVisibilityChanged,而不是 onVisibleChanged。不确定这是否是一个打字错误还是它们已经更改了函数名。 - nwp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接