1. 程式人生 > >設定QWidget關閉時觸發的事件

設定QWidget關閉時觸發的事件

 觸發關閉事件實際上就是重寫closeEvent()事件 當使用者想要關閉視窗的時候,closeEvent事件被髮送給視窗。通常是選擇選單上的“Close”按鈕,

//在標頭檔案中加入
protected:
    closeEvent(QCloseEvent *event);


//函式主體部分
void MyWidget::closeEvent(QCloseEvent *event)//此函式在QWidget關閉時執行
{
     if(trayIcon->isVisible())
         this->hide();
     else
         {
               QMessageBox::information(this,"Notice","You can't close the window");
          }
     event-

>ignore();
}

當你想讓關閉視窗的時候,讓視窗銷燬,在建構函式中設定Qt::WA_DeleteOnClose標誌:setAttribute(Qt::WA_DeleteOnClose);

帶驗證