本例是個簡單的小技巧,QMessageBox的按鍵選擇
#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
void msgbox()
{
QMessageBox box(QMessageBox::Question, "Title", "Some Imformation");
QPushButton *yesbutton = box.addButton("Yes",QMessageBox::AcceptRole);
QPushButton *nobutton = box.addButton("No",QMessageBox::AcceptRole);
//這個按鍵的作用, 是添加右上角的小紅叉關閉鍵
QPushButton *closebutton = box.addButton("Close",QMessageBox::RejectRole);
closebutton->hide();
box.exec();
if(yesbutton == box.clickedButton())
{
qDebug() << "yesbutton clicked";
}
else if (nobutton == box.clickedButton())
{
qDebug() << "nobutton clicked";
}
else
{
qDebug() << "closebutton clicked";
}
}