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. Saving content from QTextEdit in QFile to a path without a QFileDialog

Saving content from QTextEdit in QFile to a path without a QFileDialog

Scheduled Pinned Locked Moved Solved General and Desktop
qfilesavehtml
8 Posts 4 Posters 5.5k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 17 May 2016, 18:39 last edited by
    #1

    Hello everyone!

    I want to save the Content of QTextEdit to path (e.g. C:/Users/Desktop/Save). The Format of the file should be .htm

    How can i do that? I don't want to open the QFileDialog.

    This function should be used as a recovery file function. The User don't want to save the file, the program produce a file. If the user want to edit the not save file he can use the recovery file which the program automatically when the Programm closed.

    Thanks,
    Henrik

    K 1 Reply Last reply 17 May 2016, 18:52
    0
    • ? A Former User
      17 May 2016, 18:39

      Hello everyone!

      I want to save the Content of QTextEdit to path (e.g. C:/Users/Desktop/Save). The Format of the file should be .htm

      How can i do that? I don't want to open the QFileDialog.

      This function should be used as a recovery file function. The User don't want to save the file, the program produce a file. If the user want to edit the not save file he can use the recovery file which the program automatically when the Programm closed.

      Thanks,
      Henrik

      K Offline
      K Offline
      koahnig
      wrote on 17 May 2016, 18:52 last edited by
      #2

      @HenrikSt.
      QFileDialog gives you simply a name, but it does not open a file anyway.
      QFile is used to open a file. Check out the examples in the description. There is an input as well as an output example given.
      With QTextEdit::toHtml you can get the data and write to the desired file.

      Vote the answer(s) that helped you to solve your issue(s)

      ? 1 Reply Last reply 18 May 2016, 07:09
      0
      • K koahnig
        17 May 2016, 18:52

        @HenrikSt.
        QFileDialog gives you simply a name, but it does not open a file anyway.
        QFile is used to open a file. Check out the examples in the description. There is an input as well as an output example given.
        With QTextEdit::toHtml you can get the data and write to the desired file.

        ? Offline
        ? Offline
        A Former User
        wrote on 18 May 2016, 07:09 last edited by
        #3

        @koahnig
        The example is good, but how can save the file to a specific path?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 May 2016, 07:39 last edited by
          #4

          Hi,

          If you want your user to provide that path, use QFileDialog::getSaveFileName

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          ? 1 Reply Last reply 18 May 2016, 07:43
          0
          • S SGaist
            18 May 2016, 07:39

            Hi,

            If you want your user to provide that path, use QFileDialog::getSaveFileName

            ? Offline
            ? Offline
            A Former User
            wrote on 18 May 2016, 07:43 last edited by
            #5

            @SGaist
            I know but exactly this i don't want...

            J K 3 Replies Last reply 18 May 2016, 08:22
            0
            • ? A Former User
              18 May 2016, 07:43

              @SGaist
              I know but exactly this i don't want...

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 18 May 2016, 08:22 last edited by
              #6

              @HenrikSt. Did you read the QFile documentation as suggested by @koahnig ?
              http://doc.qt.io/qt-5/qfile.html
              There is an example how to open a file and to write into it:

                 QFile file("out.txt"); // Replace this path with the one you want to use
                  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                      return;
              
                  QTextStream out(&file);
                  out << "The magic number is: " << 49 << "\n";
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • ? A Former User
                18 May 2016, 07:43

                @SGaist
                I know but exactly this i don't want...

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 18 May 2016, 08:28 last edited by jsulm
                #7

                @HenrikSt. You can use QStandardPaths http://doc.qt.io/qt-5/qstandardpaths.html to get directories which are used to store specific data.

                QString path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + '/' + "file.txt";
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • ? A Former User
                  18 May 2016, 07:43

                  @SGaist
                  I know but exactly this i don't want...

                  K Offline
                  K Offline
                  koahnig
                  wrote on 18 May 2016, 11:17 last edited by koahnig
                  #8

                  @HenrikSt.

                  You got a number of different possibilities suggested. Possibly you are overwhelmed by the different things. Sometimes it is hard for the people answering to know where the real problem was/is.

                  I am not sure now if this might be too detailed for you. Anyway I move forward.

                  I would suggest, if you have not done yet, The example from QFile as also listed by @jsulm and play a bit around. Just change the file name to somthing you need. E.g.

                  QFile file("c:/data/myNewFolder/out.txt");
                  

                  That is a possibility to specify a complete (absolute) file name.

                  If you are a beginner, I was in that stage before as all the others, there might be a problem with the not existing folder name, if your file not created. There is QDir::mkPath, which is slightly different from mkDir. It may help you in code to ensure that you actually can create the file.

                  The other thing driving me personally nuts is the stupid folder separator for windows, which is a back slash '\'. However, in the mean time you can easily substitute with a forward slash also for most things in Windows (especially Qt). This also part of some of the suggestions.

                  If you have difficulties sometimes it helps for others to post a short section of code and the error message.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0

                  3/8

                  18 May 2016, 07:09

                  5 unread
                  • Login

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