Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

File saving without opening explorer



  • in notepad when i am triggering save it opens file explorer
    how to save in notepad without openning explorer
    i used this code
    void Diary::on_actionSave_triggered()
    {
    QString fileName;

    if (currentFile.isEmpty()) {
        fileName = QFileDialog::getSaveFileName(this, "Save");
        currentFile = fileName;
    } else {
        fileName = currentFile;
    }
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
        return;
    }
    setWindowTitle(fileName);
    QTextStream out(&file);
    QString text = ui->textEdit_diary->toPlainText();
    out << text;
    file.close();
    

    }


  • Lifetime Qt Champion

    Hi,

    And what would your question be ?



  • @SGaist here when i am triggering save it open file explorer i don't want that.How can i set a directory of my choice?where in this code should i change?



  • @Mahin-Abid
    If you don't want a file-explorer-type interface to be shown to the user, don't call QFileDialog::getSaveFileName(). Its sole purpose is to prompt the user to choose.

    If you want to affect the initial directory of QFileDialog::getSaveFileName(), see https://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName and use the optional const QString &dir = QString() parameter.

    If you know what directory you want to save in and only want a filename from the user, use a plain QLineEdit for that.


Log in to reply