How to convert QStringList to std::string
-
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-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
@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?
-
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; }
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. -
Hi,
Why not use QFile and write the QTextEdit content directly to it ?
-
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.@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
-
@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
@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?
-