对话框分为模态和非模态两种,模态又分为应用级别和窗口级别的。模态对话框意为只能在当前窗口下操作,而非模态可以在不同窗口间进行切换
QDialog中,使用exec()是模态对话框,show()是非模态对话框
最好创建QDialog指针,否则对话框在堆上创建,根本看不见
#include "mainwindow.h"
#include <QAction>
#include <QDialog>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setWindowTitle("Cowan's window");
openAction = new QAction(QIcon(":images/doc-open"), tr("&Open..."), this);
openAction -> setShortcuts(QKeySequence :: Open);
openAction -> setStatusTip(tr("Opening an file"));
**connect(openAction, &QAction::triggered, this, &MainWindow::open);**//别忘了进行连接!!!
QMenu* file = menuBar() -> addMenu(tr("&File"));
file -> addAction(openAction);
QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);
}
MainWindow::~MainWindow()
{
}
void MainWindow::open()
{
QDialog *dialog = new QDialog;
dialog -> setWindowTitle(tr("Hello, dialog!"));
dialog -> show();
}