模态对话框:对话框阻塞,此时只能在对话框操作,不能操作父窗口
非模态对话框:可以操作父窗口
标准对话框
1. 构造函数构建
QMessageBox message(QMessageBox::NoIcon, "title", "content", QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
message.setIconPixmap(QPixmap("icon.png"));
message.setStyleSheet("background-color:rgb(50,50,50);color: rgb(255,255,255);width:300px;font-size:28px;");
// 非模态
message.show();
// 模态
int res = message.exec();
if(QMessageBox::Yes == res) {
qDebug() << "ok";
}
2. 静态方法(about/question/warning/information)
int res = QMessageBox::about(this, "title", "content", QMessageBox::Save | QMessageBox::Cancel);
if(QMessageBox::Save == res) {
qDebug() << "save";
}
Qdialog
属性
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Dialog); // 置于所有程序的顶层
模态:
setAttribute(Qt::WA_ShowModal, true); // 属性设置
setWindowModality(Qt::WindowModal); // 设置阻塞类型
setModal(true);
Qdialog dialog;
int res = dialog.exec();
if (res == QDialog::Accepted) {
QMessageBox::(this, "INFORMATION", "You clicked OK button!");
} else if (res == QDialog::Rejected) {
QMessageBox::(this, "INFORMATION", "You clicked NO button!");
}
QDialogButtonBox
QDialogButtonBox* btnBox = new QDialogButtonBox(Qt::Horizontal); //QDialogButtonBox使用方法
btnBox->addButton(btnOK, QDialogButtonBox::ActionRole);
btnBox->addButton(btnNO, QDialogButtonBox::ActionRole);
文件对话框
// 默认打开目录"." ,后缀名过滤
QString filePath = QFileDialog::getOpenFileName(this, "title", ".", "image(*.jpg,*.png);all(*.*)");
if(filePath.length() == 0) {
QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
}