1.QMessageBox
QMessageBox
是Qt中用于显示标准对话框的类,它可以用来显示信息、警告、错误和询问用户。
#include <QMessageBox>
// ... 在某个函数或槽中
QMessageBox::critical(this, "Error", "An error occurred!");
2.自定义对话框
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
class ErrorDialog : public QDialog {
public:
ErrorDialog(QWidget *parent = nullptr) : QDialog(parent) {
QVBoxLayout *layout = new QVBoxLayout(this);
QLabel *label = new QLabel("An error occurred!", this);
QPushButton *okButton = new QPushButton("OK", this);
layout->addWidget(label);
layout->addWidget(okButton);
connect(okButton, &QPushButton::clicked, this, &ErrorDialog::accept);
}
};
// ... 在某个函数或槽中
ErrorDialog *dialog = new ErrorDialog(this);
dialog->exec();
delete dialog; // 注意:在使用exec()之后,需要手动删除对话框,防止内存泄漏