QMessageBox 使用方法

来源:互联网 发布:天策雪河军爷捏脸数据 编辑:程序博客网 时间:2024/06/05 20:59

QMessageBox普通用法

静态函数街口,直接调用,缺点是不太灵活,无法设置窗口风格

void about ( QWidget * parent, const QString & title, const QString & text )void aboutQt ( QWidget * parent, const QString & title = QString() )StandardButton  critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )StandardButton  information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )StandardButton  question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )StandardButton  warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

例子:

int ret = QMessageBox::warning(this, tr("My Application"),                                tr("The document has been modified.\n"                                   "Do you want to save your changes?"),                                QMessageBox::Save | QMessageBox::Discard                                | QMessageBox::Cancel,                                QMessageBox::Save); //使用标准按键时可以利用 exec()函数的返回值,判断用户选择 switch (ret) {   case QMessageBox::Save:       // Save was clicked       break;   case QMessageBox::Discard:       // Don't Save was clicked       break;   case QMessageBox::Cancel:       // Cancel was clicked       break;   default:       // should never be reached       break; }

QMessageBox 高级用法

有时需要自定义按键的文本,窗口关闭后需要判断用户点击的按键,代码如下:

QMessageBox msgBox(this);//保证窗口居中//设置无标题栏         msgBox.setWindowFlags( PreOrNext.windowFlags() |Qt::FramelessWindowHint);         msgBox.setIcon(QMessageBox::Question);         msgBox.setText(tr("xxxxxxxxx?"));         QPushButton *btnNxt =  msgBox.addButton(tr("确定"),QMessageBox::AcceptRole);         QPushButton *btnPre =  msgBox.addButton(tr("取消"),QMessageBox::RejectRole);         msgBox.setDefaultButton(btnNxt);         PreOrNext.exec(); //阻塞等待用户选则(模态窗口)          if (msgBox.clickedButton() == btnNxt ) {    // connect } else if (msgBox.clickedButton() == btnPre ) {    // abort }
原创粉丝点击