使用
1 int iRet = msgBox(pos, tr("警告") 2 , tr("确定要删除当前选中的行吗?") 3 , tr("确定"), tr("取消"), 1, 1); 4 if (1 == iRet) return; // 0:确定 1:取消
实现
1 //在指定的点topLeft 显示 MessageBox 2 int msgBox(const QPoint& topLeft 3 , const QString &title 4 , const QString &text 5 , const QString &button0 6 , const QString &button1 7 , const int &defaultButton 8 , const int &escapeButton 9 , QMessageBox::Icon icon 10 , QWidget *parent) 11 { 12 QMessageBox msg(icon, title, text, QMessageBox::NoButton, parent 13 , Qt::WindowStaysOnTopHint | Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint); 14 15 QPushButton* pBtn0 = NULL; 16 QPushButton* pBtn1 = NULL; 17 18 pBtn0 = msg.addButton(button0, QMessageBox::NoRole); //id = 0 19 if (!button1.isEmpty()) 20 { 21 pBtn1 = msg.addButton(button1, QMessageBox::NoRole); //id = 1 22 } 23 24 if (defaultButton == 0) msg.setDefaultButton(pBtn0); 25 if (!button1.isEmpty()) 26 { 27 if (defaultButton == 1) msg.setDefaultButton(pBtn1); 28 } 29 30 if (escapeButton == 0) msg.setEscapeButton(pBtn0); 31 if (!button1.isEmpty()) 32 { 33 if (escapeButton == 1) msg.setEscapeButton(pBtn1); 34 } 35 36 if (topLeft.x() >= 0 && topLeft.y() >= 0) msg.move(topLeft); 37 38 return msg.exec(); //返回用户点击的 按钮的 id 39 }