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. How to convert QStringList to std::string
QtWS25 Last Chance

How to convert QStringList to std::string

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.1k 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.
  • M Offline
    M Offline
    micha_eleric
    wrote on last edited by
    #1

    Trying to read QString from textEdit, break into single lines, then print to file.

    sString.split(QRegExp(QLatin1String("\n")));
    

    only thing I found to split QString, but returns QStringList, but can not find how to convert QStringList to std::string so line can be printed to file.

    QString sString;
    QStringList lineString;
    std::string fileString;
    
    sString = ui->textEdit->toPlainText();
    lineString = sString.split(QRegExp(QLatin1String("\n")));
    int i = 0;
    while (i < lineString.length() )
    {
        fileString = lineString.toStdList();
        m_myfileOut << fileString << '\n' << std::flush;
    }
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • M micha_eleric

      @Christian-Ehrlicher said in How to convert QStringList to std::string:

      Iterate over the list and convert the single strings to std::string.
      Apart from this I don't understand why you first split a string by '\n' just to convert it to combine it afterwards with '\n' again.

      the only way I found to get each line separated from a QString

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #5

      @micha_eleric This:

      fileString = lineString.toStdList();
      

      is probably intended to be:

      fileString = lineString.at(i).toStdString();
      

      to select one of the individual QStrings in the lineString list to convert.

      I think you have missed @Christian-Ehrlicher's point. Your code (corrected as above):

      • Obtains an original string
      • Splits the string into lines (using an over-complex mechanism and deprecated classes).
      • Converts each QString into a std::string
      • Writes it to a file with a trailing newline.

      The result is a file that contains the essentially same text as the original string (with the possible exception of extra new line). Why bother with all the steps in between?

      1 Reply Last reply
      3
      • M micha_eleric

        Trying to read QString from textEdit, break into single lines, then print to file.

        sString.split(QRegExp(QLatin1String("\n")));
        

        only thing I found to split QString, but returns QStringList, but can not find how to convert QStringList to std::string so line can be printed to file.

        QString sString;
        QStringList lineString;
        std::string fileString;
        
        sString = ui->textEdit->toPlainText();
        lineString = sString.split(QRegExp(QLatin1String("\n")));
        int i = 0;
        while (i < lineString.length() )
        {
            fileString = lineString.toStdList();
            m_myfileOut << fileString << '\n' << std::flush;
        }
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        Iterate over the list and convert the single strings to std::string.
        Apart from this I don't understand why you first split a string by '\n' just to convert it to combine it afterwards with '\n' again.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        M 1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi,

          Why not use QFile and write the QTextEdit content directly to it ?

          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
          3
          • Christian EhrlicherC Christian Ehrlicher

            Iterate over the list and convert the single strings to std::string.
            Apart from this I don't understand why you first split a string by '\n' just to convert it to combine it afterwards with '\n' again.

            M Offline
            M Offline
            micha_eleric
            wrote on last edited by
            #4

            @Christian-Ehrlicher said in How to convert QStringList to std::string:

            Iterate over the list and convert the single strings to std::string.
            Apart from this I don't understand why you first split a string by '\n' just to convert it to combine it afterwards with '\n' again.

            the only way I found to get each line separated from a QString

            C 1 Reply Last reply
            0
            • M micha_eleric

              @Christian-Ehrlicher said in How to convert QStringList to std::string:

              Iterate over the list and convert the single strings to std::string.
              Apart from this I don't understand why you first split a string by '\n' just to convert it to combine it afterwards with '\n' again.

              the only way I found to get each line separated from a QString

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #5

              @micha_eleric This:

              fileString = lineString.toStdList();
              

              is probably intended to be:

              fileString = lineString.at(i).toStdString();
              

              to select one of the individual QStrings in the lineString list to convert.

              I think you have missed @Christian-Ehrlicher's point. Your code (corrected as above):

              • Obtains an original string
              • Splits the string into lines (using an over-complex mechanism and deprecated classes).
              • Converts each QString into a std::string
              • Writes it to a file with a trailing newline.

              The result is a file that contains the essentially same text as the original string (with the possible exception of extra new line). Why bother with all the steps in between?

              1 Reply Last reply
              3
              • M micha_eleric has marked this topic as solved on

              • Login

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