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

后端 未结 3 1509
长情又很酷
长情又很酷 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: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 ;)

提交回复
热议问题