How can I disable Alt + F4 window closing using Qt?

后端 未结 3 1503
长情又很酷
长情又很酷 2021-02-02 16:29

I\'ve disabled X button in Qt from my dialog using this line:

myDialog->setWindowFlags(Qt::Dialog | Qt::Desktop)
         


        
相关标签:
3条回答
  • 2021-02-02 16:51

    Also you can handle the event in your dialog's class (at least if it's modal dlg):

    void MyDialog::closeEvent(QCloseEvent* e)
    {
        if ( condition )
           e->ignore();
        else
           __super::closeEvent(e);
    }
    
    0 讨论(0)
  • 2021-02-02 16:58

    The code below prevents a dialog close when pressed Alt+F4, [X] or Escape, but not by calling SomeDialog::close() method.

    void SomeDialog::closeEvent(QCloseEvent *evt) {
        evt->setAccepted( !evt->spontaneous() );
    }   
    
    void SomeDialog::keyPressEvent(QKeyEvent *evt) {
        // must be overridden but empty if the only you need is to prevent closing by Escape
    }   
    

    good luck to all of us ;)

    0 讨论(0)
  • 2021-02-02 17:13

    Pressing Alt+F4 results in a close event being sent to your top level window. In your window class, you can override closeEvent() to ignore it and prevent your application from closing.

    void MainWindow::closeEvent(QCloseEvent * event)
    {
        event->ignore();
    }
    

    If you left the close button (X) visible, this method would also disable it from closing your app.

    This is usually used to give the application a chance to decide if it wants to close or not or ask the user by displaying an "Are you sure?" message box.

    0 讨论(0)
提交回复
热议问题