問題描述:
最近做一個QT小項目,需要在登陸界面的關閉按鈕,增加一個是否確認關閉的對話框。
問題解決:
使用QMessageBox,製作對話框。
QPushButton *okbtn = new QPushButton(QString::fromLocal8Bit("確定"));
QPushButton *cancelbtn = new QPushButton(QString::fromLocal8Bit("取消"));
QMessageBox *mymsgbox = new QMessageBox;
mymsgbox->setIcon(QMessageBox::Warning);
mymsgbox->setWindowTitle(QString::fromLocal8Bit("提示"));
mymsgbox->setText(QString::fromLocal8Bit("確定關閉,登陸界面麼?"));
mymsgbox->addButton(okbtn, QMessageBox::AcceptRole);
mymsgbox->addButton(cancelbtn, QMessageBox::RejectRole);
mymsgbox->show();
mymsgbox->exec();//阻塞等待用戶輸入
if (mymsgbox->clickedButton()==okbtn)//點擊了OK按鈕
{
this->close();
}
else{
}