直接上官方文档
//提示型
static StandardButton information(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
//疑问型
static StandardButton question(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = StandardButtons(Yes | No),
StandardButton defaultButton = NoButton);
//错误型
static StandardButton warning(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
//批判型
static StandardButton critical(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
//关于型
static void about(QWidget *parent, const QString &title, const QString &text);
详解 提示型information
static StandardButton information(QWidget *parent,
const QString &title,
const QString &text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
QMessageBox::information(NULL,
QStringLiteral("提示"),
QStringLiteral("加载默认设定参数[%1]。").arg(i + 1),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes);
首先,它是static的,所以我们能够使用类名直接访问到(怎么看都像废话…);然后看它那一堆参数,参数一parent,说明它的父组件;参数二title,也就是对话框的标题;参数三text,是对话框显示的内容;参数四buttons,声明对话框放置的按钮,默认是只放置一个OK按钮(不写默认为一个ok),这个参数可以使用或运算,例如我们希望有一个Yes和一个No的按钮,可以使用QMessageBox::Yes | QMessageBox::No,所有的按钮类型可以在QMessageBox声明的StandarButton枚举中找到;参数五defaultButton就是默认选中的按钮,默认值是NoButton,也就是哪个按钮都不选中。这么多参数,豆子也是记不住的啊!所以,我们在用QtCreator写的时候,可以在输入QMessageBox::information之后输入(,稍等一下,QtCreator就会帮我们把函数签名显示在右上方了,还是挺方便的一个功能!
对话框的交互
我们使用QMessageBox类的时候有两种方式,一种是使用static函数,另一种是使用构造函数。
首先来说一下static函数的方式。注意,static函数都是要返回一个StandardButton,我们就可以通过判断这个返回值来对用户的操作做出相应。
QMessageBox::StandardButton rb = QMessageBox::question(NULL, "Show Qt", "Do you want to show Qt dialog?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(rb == QMessageBox::Yes)
{
QMessageBox::aboutQt(NULL, "About Qt");
}
如果要使用构造函数的方式,那么我们就要自己运行判断一下啦:
QMessageBox message(QMessageBox::NoIcon, "Show Qt", "Do you want to show Qt dialog?", QMessageBox::Yes | QMessageBox::No, NULL);
if(message.exec() == QMessageBox::Yes) //典型模态模式
{
QMessageBox::aboutQt(NULL, "About Qt");
}
详解Demo
void CSceCreator::onNewScn()
{
//提示保存数据
if (m_scenarioMgr->isScnLoaded())
{
QString title = LSTR("新建方案");
QString sinfo = LSTR("当前所有没有保存的数据都会丢失,您确定吗?");
QMessageBox msgBox(QMessageBox::Warning, title, sinfo, NULL);//这里使用的是 构造函数方法
QPushButton* btn_Yes = msgBox.addButton(LSTR("是"), QMessageBox::AcceptRole);
QPushButton* btn_No = msgBox.addButton(LSTR("否"), QMessageBox::RejectRole);
msgBox.exec();
if (msgBox.clickedButton() == btn_No)
return;
//重置想定
m_scenarioMgr->resetScenario();
//重置界面显示
m_scnConfDock->resetScnTreeView();
//重置GIS显示
m_gisViewScene->clearGisProject();
}
//新建方案
CDlgNewProj dlgNP(CForceForm::FORCE_ROLE_INSCN);
if (QDialog::Accepted != dlgNP.exec())
return;
//设置想定信息
m_scenarioMgr->m_curScnInfo.name = dlgNP.m_strProjName;
m_scenarioMgr->m_curScnInfo.path = dlgNP.m_strProjPath;
m_scenarioMgr->m_curScnInfo.desc = dlgNP.m_strProjDesc;
//按步骤创建
if (1 == dlgNP.m_nCreateType)
{
CDlgSceneSet dlgSS;
CDlgFormSet dlgFS;
CDlgEvalSet dlgES;
CDlgDeplSet dlgDS;
CDlgStepSet* dlgSet[4];
int curDlgInd = 0;
bool enableFinish = false;
dlgSet[0] = &dlgSS;
dlgSet[1] = &dlgFS;
dlgSet[2] = &dlgES;
dlgSet[3] = &dlgDS;
while (true)
{
dlgSet[curDlgInd]->setEnableFinish(enableFinish);
int ret = dlgSet[curDlgInd]->exec();
//如果选择取消,直接退出
if (QDialog::Rejected == ret)
return;
if (QDialog::Accepted == ret)
{
//如果选择完成,退出循环
if (dlgSet[curDlgInd]->m_bSetpFinish)
break;
if (dlgSet[curDlgInd]->m_bStepPrev)
{
dlgSet[curDlgInd]->resetStepFlag();
curDlgInd = (curDlgInd <= 0) ? 0 : (--curDlgInd);
}
if (dlgSet[curDlgInd]->m_bStepNext)
{
dlgSet[curDlgInd]->resetStepFlag();
curDlgInd = (curDlgInd >= 3) ? 3 : (++curDlgInd);
}
}
if (curDlgInd >= 3)
enableFinish = true;
}
//根据步骤设定对话框返回,在后台加载相应的场景、编成和评估方案
int sid = dlgSS.getCurSceneId();
m_sceneMgr->openScnScene(sid);
int rfid, bfid;
dlgFS.getCurFormId(rfid, bfid);
m_forceMgr->loadFormPrj(CForceForm::FORCE_COLOR_RED, rfid);
m_forceMgr->loadFormPrj(CForceForm::FORCE_COLOR_BLUE, bfid);
}
//通知想定视图更新想定树
m_scnConfDock->updateScnTreeView();
//变更想定后,设置运行态为空状态(即运行时需要重新下发想定文件)
_changeButtonState(sysrun_state_none);
//日志记录
CScenarioLog::scenarioLog()->writeLog(CScenarioLog::log_Sys, LSTR("新建") + m_scenarioMgr->m_curScnInfo.path);
}