Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] getSaveFileName()
Qt 6.11 is out! See what's new in the release blog

[SOLVED] getSaveFileName()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CoreyWhiteC Offline
    CoreyWhiteC Offline
    CoreyWhite
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • CoreyWhiteC Offline
        CoreyWhiteC Offline
        CoreyWhite
        wrote on last edited by
        #3

        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();
        }

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ckakman
          wrote on last edited by
          #4

          [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.

          1 Reply Last reply
          0
          • CoreyWhiteC Offline
            CoreyWhiteC Offline
            CoreyWhite
            wrote on last edited by
            #5

            Could you provide an example of this, please?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ckakman
              wrote on last edited by
              #6

              [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() )@

              1 Reply Last reply
              0
              • CoreyWhiteC Offline
                CoreyWhiteC Offline
                CoreyWhite
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved