QString::fromWCharArray avoiding new line char "\n" - qt

Received wchar_t message from server as "TextmesageComing\nSomeValueObserveany\ntruncationOccuringonthescreenwhiletestingthescenation"
so i used QString converter
QString textName = QString::fromWCharArray(smartAlertMessages_.text);
to draw text on QPainter drawText
painter->drawText(messageRect, Qt::TextWordWrap | Qt::TextWrapAnywhere, text);
but observed the "\n" characters are not considered as new line and start displaying \n as a part of the text .
Same text if i hard code in drawText is working properly.

Related

Qt lineEdit text conversion to hex issue

I want to take the text from a lineEdit widget and convert it to hex.
I thought that the two following conversions have the same result but they don't.
1. data = QByteArray::fromHex(ui->lineEdit_send->text().toUtf8().toHex());
qDebug() << data;
2. data = QByteArray::fromHex(ui->lineEdit_send->text().toUtf8());
data = data.toHex();
qDebug() << data;
For example, if I enter the text "$54$65", the first implementation outputs $54$65 while the second implementation outputs "5465" and I cannot understand why the '$' is not displayed.

How to set character encoding for QTextBrowser in Qt?

I have a QTextBrowser in which I display the output contents of an external binary using QProcess in Linux. All is GOOD! But most of the contents are just boxes, so now it's the character encoding UTF-8 is missing and I need to tell this to the QTextBrowser. Is there any way for that?
The code:
....
processRAM = new QProcess();
processRAM->start("memtester", QStringList() << "1" << "1");
.....
connect(processRAM, SIGNAL(readyRead()),this,SLOT(displayRAMTestOutput()));
......
void MainWindow::displayRAMTestOutput()
{
textBrowserData->append(Qtring::fromUtf8(processRAM->readAllStandardOutput())));
}
I added the char encoding UTF-8 and still I see only boxes. What am I missing here?
You can set content of QTextBrowser in this way:
textBrowser->setText(QString::fromUtf8(processOutput)));
EDIT:
Your problem with "boxes" isn't connected with UTF8 encoding. Symbols which you see are control characters which are used by memtester when it displays text to console. If you don't want to display such characters in textBrowser, you can filter output:
while(!processRAM->atEnd())
{
QString out = QString::fromAscii(processRAM->readLine());
if(!out.contains("\b"))
textBrowser->append(out);}
}
\b means backspace which is displayed in you textBrowser as boxes.

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();

How to make a QString from a QTextStream?

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.

Qt. get part of QString

I want to get QString from another QString, when I know necessary indexes.
For example:
Main string: "This is a string".
I want to create new QString from first 5 symbols and get "This ".
input : first and last char number.
output : new QString.
How to create it ?
P.S. Not only first several letters, also from the middle of the line, for example from 5 till 8.
If you do not need to modify the substring, then you can use QStringRef. The QStringRef class is a read only wrapper around an existing QString that references a substring within the existing string. This gives much better performance than creating a new QString object to contain the sub-string. E.g.
QString myString("This is a string");
QStringRef subString(&myString, 5, 2); // subString contains "is"
If you do need to modify the substring, then left(), mid() and right() will do what you need...
QString myString("This is a string");
QString subString = myString.mid(5,2); // subString contains "is"
subString.append("n't"); // subString contains "isn't"
Use the left function:
QString yourString = "This is a string";
QString leftSide = yourString.left(5);
qDebug() << leftSide; // output "This "
Also have a look at mid() if you want more control.

Resources