Convert TCHAR* to QString - qt

How to convert easiest way in Qt?
int recordSize = 1000;
TCHAR* qRecord = new TCHAR[recordSize];
//here I get data form other function
//here I try to display
qString() << QString::fromWCharArray(qRecord,recordSize);//gives many ????
printf("%s",qRecord); // this work perfectly
I tried with wcstombs, formStdWString nad other but nothing seems to work.
Thanks for any help

QString s= (LPSTR)qRecord;
worked.
thanks

#kajojeq
no, your second answer is not correct. because when the encoding is set to utf16(or even utf8 sometimes) the s variable only save one character.
correct conversion is:
QString str = QString::fromWCharArray(qrecord)

Related

How to read numbers; How to store those numbers in variable; and how to display those numbers in a label; from line edit in Qt?

I have to read numbers from line edit in Qt Creator, and then divide them by 100 and display them in a label.
The only method I know is:
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
ui->label_OOP_marks->setText(QString(OOP_marks));
But the above method cannot read numbers; it reads string. I have tried a lot but can't figure out the code for this part of the program.
You can use QString::toDouble for converting string to number. If you are planning to accept integers use QString::toInt instead. To convert number to string again QString::number is way to go.
const auto& OOP_marks = ui->lineEdit_OOP_marks_input->text();
bool isNumber; // optional
const double number = OOP_marks.toDouble(&isNumber);
const auto& result = isNumber ? QString::number(number / 100) : QString("Please enter valid number.");
ui->label_OOP_marks->setText(result);
But I suggest you to use QSpinBox or QDoubleSpinBox, instead of QLineEdit.
How about QString toInt function, and then static QString::number function?
QString OOP_marks = ui->lineEdit_OOP_marks_input->text();
int OOP_marks_val = OOP_marks.toInt();
ui->label_OOP_marks->setText(QString::number(OOP_marks_val/100));

Problems in converting to UTF-8 in Qt

I try to show a persian string in Qt:
QMessageBox msg;
QString str = "یا حسین";
msg.setText(QString::fromUtf8(str));
msg.exec();
but it shows the following error :
/home/msi/Desktop/VoMail
Project/Project/VoMail-build-desktop-Qt_4_8_1_in_PATH__System__Release/../VoMail/mainwindow.cpp:40:
error: no matching function for call to 'QString::fromUtf8(QString&)'
I want to use a string variable, and not a string directly.
How can I convert a QString variable to Utf8?
As seen here, QString::fromUtf8() does not accept an argument of type QString. You must give it a const char *, so you could rewrite it like this:
QMessageBox msg;
QString str = QString::fromUtf8("یا حسین");
msg.setText(str);
msg.exec();
its not good idea write like that
using this must be better
QString str(tr("ya hossein");
and use linguist and add persian translation file to your project http://qt-project.org/doc/qt-4.8/linguist-translators.html
and if you dont want use this, you must be sure your IDE or code editor (like qtcreator) use utf8 for saving files and just use
QString str("یا حسین");
it must be ok, i tested that so many times

QString function for only alpha characters?

I am newbie in QT(4.7.4) and I am search for function, that checks an QString for alpha-characters and returns "true" if in this QString contains only characters.
Should I write this simple function myself? :( I hope it exists such function as isText() in VBA, but in Google and documentation I have not found it.
Thanks for answers and sorry for my english :)
You can simply validate the string with a QRegExp class matching an alphanumeric string. I suggest to use it with QValidator to be more clear.
You could use something like this (If your goal is to accept only strings, which contains a single character):
bool containsOnly(QString str, QChar c)
{
for(int i=0; i<str.length(); i++)
if(str.at(i)!=c)
return false;
return true;
}
and in use:
bool b = containsOnly("String", 'a');

Object back from pointer

void show(QString *s){
//Here I want to show the value of the QString.
}
How can I do that??
I'd be glad if you could help me.
Not sure exactly what you're asking - see my comments.
Maybe this will help?
Check out toAscii(), toLatin1(), toUtf8()
const char* data = s->toAscii(); // if you want ASCII encoding
data = s->toUtf8(); // if you want UTF-8 encoding
// etc.

QByteArray to integer

As you may have figured out from the title, I'm having problems converting a QByteArray to an integer.
QByteArray buffer = server->read(8192);
QByteArray q_size = buffer.mid(0, 2);
int size = q_size.toInt();
However, size is 0. The buffer doesn't receive any ASCII character and I believe the toInt() function won't work if it's not an ASCII character. The int size should be 37 (0x25), but - as I have said - it's 0.
The q_size is 0x2500 (or the other endianness order - 0x0025).
What's the problem here ? I'm pretty sure q_size holds the data I need.
Something like this should work, using a data stream to read from the buffer:
QDataStream ds(buffer);
short size; // Since the size you're trying to read appears to be 2 bytes
ds >> size;
// You can continue reading more data from the stream here
The toInt method parses a int if the QByteArray contains a string with digits. You want to interpret the raw bits as an integer. I don't think there is a method for that in QByteArray, so you'll have to construct the value yourself from the single bytes. Probably something like this will work:
int size = (static_cast<unsigned int>(q_size[0]) & 0xFF) << 8
+ (static_cast<unsigned int>(q_size[1]) & 0xFF);
(Or the other way around, depending on Endianness)
I haven't tried this myself to see if it works but it looks from the Qt docs like you want a QDataStream. This supports extracting all the basic C++ types and can be created wth a QByteArray as input.
bool ok;
q_size.toHex().toInt(&ok, 16);
works for me
I had great problems in converting serial data (QByteArray) to integer which was meant to be used as the value for a Progress Bar, but solved it in a very simple way:
QByteArray data = serial->readall();
QString data2 = tr(data); //converted the byte array to a string
ui->QProgressBar->setValue(data2.toUInt()); //converted the string to an unmarked integer..
This works for me:
QByteArray array2;
array2.reserve(4);
array2[0] = data[1];
array2[1] = data[2];
array2[2] = data[3];
array2[3] = data[4];
memcpy(&blockSize, array2, sizeof(int));
data is a qbytearray, from index = 1 to 4 are array integer.
Create a QDataStream that operates on your QByteArray. Documentation is here
Try toInt(bool *ok = Q_NULLPTR, int base = 10) const method of QByteArray Class.
QByteArray Documentatio: http://doc.qt.io/qt-5/QByteArray.html

Resources