美文网首页
QFileDialog

QFileDialog

作者: downdemo | 来源:发表于2018-08-10 16:51 被阅读44次

QFileDialog

#include  "qfiledialog.h"
void MainWindow::slot()
{
    // 保存用getSaveFileName
    QString path = QFileDialog::getOpenFileName(this, tr("Open File"), "C:/Users/downdemo/Desktop", tr("Text File(*.txt)"));
    // std::string str = path.toStdString());
    if(!path.isEmpty())
    {
        lineEdit->setText(path);
        lineEdit->setFocus();
        lineEdit->selectAll();
        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."));
    }
}

相关文章

网友评论

      本文标题:QFileDialog

      本文链接:https://www.haomeiwen.com/subject/tjllbftx.html