QFileDialog

2018-08-10  本文已影响44人  downdemo

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."));
    }
}
上一篇 下一篇

猜你喜欢

热点阅读