messageBox
设置messageBox信息
这种设置方式一般在我们需要仔细设置messageBox 的信息,
void MainWindow::on_pushButton_clicked()
{
//创建一个对话框
QMessageBox* messageBox = new QMessageBox();
messageBox->setWindowTitle("这是一个对话框标题");
messageBox->setText("这是一个对话框文本");
messageBox->setIcon(QMessageBox::Information);
messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::Save);
//除了上述设置按钮的方法,还有可以自定义设置按钮的方法
QPushButton* button = new QPushButton("Ok");
messageBox->addButton(button,QMessageBox::AcceptRole);
//模态的到这里就会阻塞,所以直接delete和调用函数都可以
int result = messageBox->exec();
//直接delete
//delete messageBox;
//调用函数
messageBox->setAttribute(Qt::WA_DeleteOnClose);
//对对话框的返回值进行反馈
if(result == QMessageBox::Ok)
{
qDebug() << "Ok";
}
else if(result == QMessageBox::Save)
{
qDebug() << "Save";
}
else if(result == QMessageBox::Cancel)
{
qDebug() << "Cancel";
}
}
通过函数设置messageBox信息,QMessageBox提供的静态函数,只能显示出简单对话框,需要详细设计请看上面
void MainWindow::on_pushButton_clicked()
{
//静态函数创建message
int result = QMessageBox::warning(this, "对话框标题", "对话框文本", QMessageBox::Ok|QMessageBox::Cancel);
if(result == QMessageBox::Ok)
{
qDebug()<<"Ok";
}
else if(result == QMessageBox::Cancel)
{
qDebug()<<"Cancel";
}
}