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. Writing a QString value in a QFile plain-text object
Forum Updated to NodeBB v4.3 + New Features

Writing a QString value in a QFile plain-text object

Scheduled Pinned Locked Moved General and Desktop
qstringqfile
3 Posts 3 Posters 23.4k 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.
  • X Offline
    X Offline
    xtingray
    wrote on 22 May 2015, 17:15 last edited by xtingray
    #1

    I wonder how hard is to write a QString value to a plain-text file using Qt. It should be an easy task, but as far as I have been looking for it isn't.
    All the code I could find is related to const char * strings. By the way, I need to use a QTextStream global variable to save the strings. Any suggestion?

    I really appreciate any example to understand what is the right way to do this.

    Thanks!


    Qt Developer

    S 1 Reply Last reply 22 May 2015, 19:30
    0
    • X xtingray
      22 May 2015, 17:15

      I wonder how hard is to write a QString value to a plain-text file using Qt. It should be an easy task, but as far as I have been looking for it isn't.
      All the code I could find is related to const char * strings. By the way, I need to use a QTextStream global variable to save the strings. Any suggestion?

      I really appreciate any example to understand what is the right way to do this.

      Thanks!

      S Offline
      S Offline
      SysTech
      wrote on 22 May 2015, 19:30 last edited by SysTech
      #2

      @xtingray

      I honestly haven't tried QFile directly but using QTextStream makes it pretty easy. Here is example code I use to save macro code which comes across as a QString:

      if( fileName.isEmpty()==false )
      {
        QFile scriptFile(fileName);
        if( !scriptFile.open(QIODevice::WriteOnly) )
        {
          QString msg = QString( tr("SaveCurrentMacro - Unable to save current script to file: %1") ).arg(fileName);
          Logger::GetLogger()->WriteToLog( msg );
          QMessageBox::critical(this, tr("Macro Editor"), msg, QMessageBox::Ok );
          return;
        }
      
        // Save the script
        QTextStream outputStream(&scriptFile);
        QString code = m_MacroEditor->toPlainText();
        code.replace("\n","\r\n");
        outputStream << code;
        scriptFile.close();
        SetCurrentMacroFile( fileName );
        SetMacroChanged( false );
      }
      
      1 Reply Last reply
      0
      • K Offline
        K Offline
        KiwiJeff
        wrote on 22 May 2015, 21:06 last edited by
        #3

        You can open the file for writing and insert write QByteArrays to it via:
        QFile::write(const QByteArray & byteArray)

        Thus pick your favorite:
        QString::toUtf8() or QString::toLocal8bit().

        1 Reply Last reply
        0

        1/3

        22 May 2015, 17:15

        • Login

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