Qt学习笔记(十)单文档界面

2018-04-22  本文已影响0人  行走行囊

界面设计


界面.png

1、 打开文件/读文件

void MyToolWindow::OnClickedOpen() {
    QString filePath = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("打开文件"));
    string filename = filePath.toStdString();
    FILE *fp = fopen(filename.c_str(), "rb");
    char buf[512];
    int n = fread(buf, 1, 512, fp);
    buf[n] = 0;
    fclose(fp);
    QString text = QString::fromStdString(buf);
    ui.plainTextEdit->setPlainText(text);
}

2、 保存文件/写文件

void MyToolWindow::OnClickedSave() {
    QString filePath = QFileDialog::getSaveFileName(this, QString::fromLocal8Bit("保存"));
    if (filePath.length() == 0) return;
    string text = ui.plainTextEdit->toPlainText().toStdString();
    string filename = filePath.toStdString();
    FILE *fp = fopen(filename.c_str(), "wb");
    fwrite(text.c_str(), 1, text.length(), fp);
    fclose(fp);
}
上一篇下一篇

猜你喜欢

热点阅读