QT提供了五个标准对话框接口,分别是critical、information、warning、question、about。当然我们也可以自己定义对话框。下面将一一详细介绍使用方法。
StandardButton 的意思是可以给,critical,information,question ,warning添加想要的类型的Button
Critical:
QMessageBox::StandardButton test;
test:QMessageBox::critical(this,tr("错误"),tr("错误原因"), QMessageBox::Yes | QMessageBox::No);
if(test == QMessageBox::Yes)
{
}
else
{
}
Information:
QMessageBox::information(this, "Title", "Text");
Warning:
QMessageBox::StandardButton test;
test:QMessageBox::warning(this,tr("警告"),tr("密码错误!"),QMessageBox::Yes | QMessageBox::No);
if(test == QMessageBox::Yes)
{
}
else
{
}
Question:
QMessageBox::StandardButton test;
test = QMessageBox::question(this, "Title", "确定?", QMessageBox::Yes | QMessageBox::No);
if(test == QMessageBox::Yes)
{
}
else
{
}
About:about是没有双按钮的,并且其支持HTML标签。
QMessageBox::about(this, "About", " <font color='blue'>信息</font>");
自定义QMessageBox:因为这是个模态对话框,需要有它自己的事件循环,所以我们用exec(),而不是用show()。
QMessageBox msg;
msg.setWindowTitle(tr("Title"));
msg.setText(tr("Text"));
msg.setIcon(QMessageBox::Information);
// msg.setIconPixmap(QPixmap("1.png"));
msg.addButton(tr("NO"),QMessageBox::NoRole);
msg.addButton(tr("YES"),QMessageBox::YesRole);
if(msg.exec())
{
QMessageBox::about(this, "About", " <font color='blue'>信息</font>");
}
else
this->close();
附上描述:
Constant Value Description
QMessageBox::InvalidRole -1 The button is invalid.
QMessageBox::AcceptRole 0 Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole 1 Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole 2 Clicking the button causes a destructive change (e.g. for Discarding Changes)
and closes the dialog.
QMessageBox::ActionRole 3 Clicking the button causes changes to the elements within the dialog.
QMessageBox::HelpRole 4 The button can be clicked to request help.
QMessageBox::YesRole 5 The button is a "Yes"-like button.
QMessageBox::NoRole 6 The button is a "No"-like button.
QMessageBox::ApplyRole 8 The button applies current changes.
QMessageBox::ResetRole 7 The button resets the dialog's fields to default values.
中文显示(编码):
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));