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"));