[SOLVED] getSaveFileName()



  • I am using: QString QFileDialog::getSaveFileName()
    It opens up a dialog and lets me select the file to save. The problem I am having is putting content into the saved file. I want it to come right out of a textEdit box. Any suggestions? A simple example would really help!



  • Hi,

    getSaveFileName() gives you just a name. You need open the file and write into it using "QFile":http://doc.qt.io/qt-5/qfile.html.

    If your question is about how to keep the formatting of the text in the QTextEdit, than that's a different problem. Your "options":http://doc.qt.io/qt-5/qtextdocumentwriter.html#supportedDocumentFormats are between HTML, plain text and proababy not up-to-date ODF. It is of course obvious that plain text doesn't keep any formatting except paragraphs.



  • Does this look like it will work?

    #include <QtCore>
    #include <QtGui>

    QString mFilename;

    QString file = QFileDialog::getSaveFileName(this,"Save a file");

    if(!file.isEmpty())
    {
    mFilename = file;
    }

    QFile sFile(mFilename);
    if(sFile.open(QFile::WriteOnly | Qfile::Text)
    {
    QTextStream out(&sFile);

    out << ui->textEdit->toPlainText();

    sFile.flush();
    sFile.close();
    }



  • [quote author="CoreyWhite" date="1420307652"]Does this look like it will work?

    #include <QtCore>
    #include <QtGui>

    QString mFilename;

    QString file = QFileDialog::getSaveFileName(this,"Save a file");

    if(!file.isEmpty())
    {
    mFilename = file;
    }

    QFile sFile(mFilename);
    if(sFile.open(QFile::WriteOnly | Qfile::Text)
    {
    QTextStream out(&sFile);

    out << ui->textEdit->toPlainText();

    sFile.flush();
    sFile.close();
    }[/quote]

    It looks OK. QFile::write() should be enough, you don't need QTextStream. Also no need to call flush() or close() since QFile destructor will do both.



  • Could you provide an example of this, please?



  • [quote author="CoreyWhite" date="1420311783"]Could you provide an example of this, please?[/quote]

    I didn't understand what you meant by "example of this". If you meant not using QTextStream:
    @sFile.write( ui->textEdit->toPlainText().toUtf8() )@



  • Yes, that is what I was wanting to see. Very cool!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.