# p03/ch06 — Qt 示例

## 可运行示例

| 目标 | 源文件 | 说明 |
|---|---|---|
| `modal_dialog` | `modal_dialog.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh modal_dialog` |
| `modeless_dialog_stack` | `modeless_dialog_stack.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh modeless_dialog_stack` |
| `modeless_dialog_heap` | `modeless_dialog_heap.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh modeless_dialog_heap` |
| `modeless_dialog_deleteonclose` | `modeless_dialog_deleteonclose.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh modeless_dialog_deleteonclose` |
| `message_box_question` | `message_box_question.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh message_box_question` |
| `message_box_custom` | `message_box_custom.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh message_box_custom` |
| `file_dialog` | `file_dialog.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh file_dialog` |

## 参考片段（签名/伪代码，未单独建目标）

### [block 4] .4 消息对话框

```cpp
void about(QWidget * parent, const QString & title, const QString & text)
```

### [block 5] .4 消息对话框

```cpp
void aboutQt(QWidget * parent, const QString & title = QString())：
```

### [block 6] .4 消息对话框

```cpp
StandardButton critical(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)：
```

### [block 7] .4 消息对话框

```cpp
StandardButton information(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)
```

### [block 8] .4 消息对话框

```cpp
StandardButton question(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = StandardButtons( Yes | No ),
StandardButton defaultButton = NoButton)
```

### [block 9] .4 消息对话框

```cpp
StandardButton warning(QWidget * parent,
const QString & title,
const QString & text,
StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)
```

### [block 13] 6.5 标准文件对话框

```cpp
connect(openAction, &QAction::triggered, 
this, &MainWindow::openFile);
connect(saveAction, &QAction::triggered, 
this, &MainWindow::saveFile);
```

### [block 14] 6.5 标准文件对话框

```cpp
//打开文件
void MainWindow::openFile()
{
    QString path = QFileDialog::getOpenFileName(this,
               tr("Open File"), ".", tr("Text Files(*.txt)"));
    if(!path.isEmpty()) 
{
        QFile file(path);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) 
{
            QMessageBox::warning(this, tr("Read File"),
                         tr("Cannot open file:\n%1").arg(path));
            return;
        }
        QTextStream in(&file);
        textEdit->setText(in.readAll());
        file.close();
    } 
else 
{
        QMessageBox::warning(this, tr("Path"),
                             tr("You did not select any file."));
     }
}

//保存文件
void MainWindow::saveFile()
{
    QString path = QFileDialog::getSaveFileName(this,
               tr("Open File"), ".", tr("Text Files(*.txt)"));
    if(!path.isEmpty()) 
{
        QFile file(path);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) 
{
            QMessageBox::warning(this, tr("Write File"),
                         tr("Cannot open file:\n%1").arg(path));
            return;
        }
        QTextStream out(&file);
        out << textEdit->toPlainText();
        file.close();
    } 
else 
{
        QMessageBox::warning(this, tr("Path"),
                             tr("You did not select any file."));
    }
}
```

### [block 15] 6.5 标准文件对话框

```cpp
QString getOpenFileName(QWidget * parent = 0,
                        const QString & caption = QString(),
                        const QString & dir = QString(),
                        const QString & filter = QString(),
                        QString * selectedFilter = 0,
                        Options options = 0)
```

