QMessageBox按鍵選擇

本例是個簡單的小技巧,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";
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章