How to make a QString from a QTextStream? - qt

Will this work?
QString bozo;
QFile filevar("sometextfile.txt");
QTextStream in(&filevar);
while(!in.atEnd()) {
QString line = in.readLine();
bozo = bozo + line;
}
filevar.close();
Will bozo be the entirety of sometextfile.txt?

Why even read line by line? You could optimize it a little more and reduce unnecessary re-allocations of the string as you add lines to it:
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
QString text;
text = in.readAll();
file.close();

As ddriver mentions, you should first open the file using file.open(…); Other than that, yes bozo will contain the entirety of the file using the code you have.
One thing to note in ddriver's code is that text.reserve(file.size()); is unnecessary because on the following line:
text = in.readAll();
This will replace text with a new string so the call to text.reserve(file.size()); would have just done unused work.

Related

Convert QTextStream to QByteArray

I need to convert a QTextStream to a QByteArray, and then back again. I found an example of QTextStream -> QByteArray by constructing a QTextStream(QBytearray) and then any text < < to the stream ends up in the bytearray.
But how about the other way? Probably a one liner but I can figure it out. Can someone post and explain?
Check it out if your text stream works via file (maybe via socket):
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString text;
text = in.readAll();
file.close();
text.QString::toUtf8(); //convert your data to byte array
To get your data back use: QString::fromUtf8(const QByteArray &str)

Qt5: Problems with german Umlaute (ä, ö, ü char) and QStrings

I have a problem with Qt5, QStrings and german Umlaute like ä, ö and ü-char. I read data of a 1024 byte header, display it in a form in my app and edit some entries. Everytime I write a german Umlaut in a QLineEdit and process it, I get 2 Bytes instead of 1 Byte - because QString does need 2 Bytes for each character since Qt5.
I've tried this:
QString str = rs_Position1_QLE->text();
QByteArray ba = str.toLatin1();
qDebug() << "ba:" << ba;
Console: ba: "S\xF6ndenlabor" the \xF6 stands for the char ö
(saving the file with the header results in EF BF BD for the ö instead of F6)
but even then I get 2 Bytes for every character with an ASCII Code higher than 127.
How can I solve my problem?
Thank you very much for your answers!
Ralf
Sorry for the delay! I've made a little example script and hope that helps:
Stringtest::Stringtest(QWidget *parent)
: QMainWindow(parent) {
this->setWindowTitle("Stringtest");
QWidget *window = new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
QPushButton *OpenFile_QPB = new QPushButton("Open File");
OpenFile_QPB->setFixedSize(100, 50);
Content_QLE = new QLineEdit;
Content_QLE->setMaxLength(20);
Content_QLE->setMinimumWidth(200);
Content_QLE->setMaximumWidth(200);
Content_QLE->setStyleSheet("padding: 5px; font-family: 'Segoe UI', 'Arial', 'Verdana'; font-size: 12px; background-color: white;
border: none; ");
QPushButton *SaveToFile_QPB = new QPushButton("Save to File");
SaveToFile_QPB->setFixedSize(100, 50);
layout->addWidget(OpenFile_QPB);
layout->addWidget(Content_QLE);
layout->addWidget(SaveToFile_QPB);
window->setLayout(layout);
this->setCentralWidget(window);
connect(OpenFile_QPB, SIGNAL(clicked()), this, SLOT(OpenFile()));
}
Stringtest::~Stringtest() { }
void
Stringtest::OpenFile() {
QString fileName = QFileDialog::getOpenFileName(this, "Open file", "C://Semroch.R", "Textfiles (*.txt);; All files (*.*)");
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
file.open(QFile::ReadOnly);
qDebug() << "system name:" << QLocale::system().name();
qDebug() << "codecForLocale:" << QTextCodec::codecForLocale()->name();
while (!file.atEnd()) {
QByteArray qbaLine = file.readLine();
qDebug() << qbaLine;
Content_QLE->setText(qbaLine);
// QString strLine = qbaLine;
// Content_QLE->setText(strLine);
}
}
} // End of void Stringtest::OpenFile()
I have a test.txt, which I load into the script - the content of the textfile is abcdefä.
The display in the QLineEdit is:
abcdef and a question mark in a diamond.
The qDebug() shows me
system name: "de_DE"
codecForLocale: "System"
in the console.
Any ideas? Thank you very much, Ralf
Few possibilities:
Be sure your shell handles utf-8 (if echo -e '\xe2\x82\xac' command doesn't show a €, then you should fix your shell)
Be sure to handle UTF-8 correctly in your application (add those lines in your main() function):
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
First thing to understand is that QByteArray is just a collection of bytes, it says nothing about its content or the encoding used, while QString is a unicode string stored as UTF-16.
So when you do Content_QLE->setText(qbaLine) or QString strLine = qbaLine, you are implicitly converting a QByteArray to a QString. According to QString documentation, it calls QString::fromUtf8().
So if your file is not encoded as UTF-8, but as Latin 1, you will have issues.
If your file is in Latin 1, you should do: Content_QLE->setText(QString::fromLatin1(qbaLine))
The same goes, when you are writing a file:
file.write(str.toUtf8()); // Write to a UTF-8 file
file.write(str.toLatin1()); // Write to a Latin 1 file

How to can append the value from txt file to the Qlist

How I can to insert the value from txt file in to the Qlist...
QList<QString> list_StRead;
list_StRead.insert();
I can sorting txt file ... its mean that my file is a line by line. than after the insert to the Qlist I want to write in to Qtabelewidget. How I must to do?? u must to be completely understand. see the img file
tnx for all....
Here is the pseudo code.
Please try it. (Not tested, code written in notepad. excuse me any syntax errors).
//Your list
QList<QString> list_StRead;
//Text stream object read data and insert in list.
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine(); //read one line at a time
list_StRead.push_back(line);
}
//loop your list
for(int i =0; i<list_StRead.size(); i++)
{
//Add by create your tablewidget item and append it.
YourTableWidget->setItem(rowNumber,colNumber,new QTableWidgetItem(list_StRead.at(i)))
}

Qt - QFile - How to read only first word in every line

How can I read only first word in every line in a text file while using QFile in Qt?
Thanks.
use
QFile ifile("in.txt");
QString text = txStream.readLine();
QStringList splitline = text.split(" ");
QFile ofile("out.txt");
ofile.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&ofile);
// join QStringList by "\n" to write each single word in an own line
out << splitline.join("\n");
ofile.close();

read a text file to QStringList

I have a text file. I need to read it to a QStringList. there are no line seperators. I mean each line in the text file is in a new line. So is there anyway i can do this?
I assume that every line should be a separate string in the list. Use QTextStream::readLine() in a cycle and on each step append the returned value to the QStringList. Like this:
QStringList stringList;
QFile textFile;
//... (open the file for reading, etc.)
QTextStream textStream(&textFile);
while (true)
{
QString line = textStream.readLine();
if (line.isNull())
break;
else
stringList.append(line);
}
QFile TextFile;
//Open file for reading
QStringList SL;
while(!TextFile.atEnd())
SL.append(TextFile.readLine());
If the file isn't too big, read the whole content into a QString and then split() it into a QStringList.
I like using the QRegExp version to handle linefeed from different platforms:
QStringList sList = s.split(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), QString::SkipEmptyParts);
I like my code to be fully indented/paranthesized with obvious variable names (they may take longer to type but are much easier to debug) so would do the following (but changing "myTextFile" and "myStringList" to more sensible names, such as "employeeListTextFile")
QFile myTextFile;
QStringList myStringList;
if (!myTextFile.open(QIODevice::ReadOnly))
{
QMessageBox::information(0, "Error opening file", myTextFile.errorString());
}
else
{
while(!myTextFile.atEnd())
{
myStringList.append(myTextFile.readLine());
}
myTextFile.close();
}
The below code reads the file
QFile File("/file_path");
if(!File.open(QIODevice::ReadOnly));
{
qDebug("Error");
}
QTextStream in(&File);
while(!in.atEnd())
{
qDebug()<<ReadAll;
ReadAll=in.readAll();
}
File.close();
Now the file is closed, now split the new line i.e \n here \r is carriage return
List= ReadAll.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);

Resources