Object back from pointer - pointers

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.

Related

Need help on understanding Writefile

I would like your assistance to understand a bit of code that would hugely help me in my project. Without going into too much details, here is what is causing me so much problems :
bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
DWORD bytesSend;
//Try to write the buffer on the Serial port
if(!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
{
//In case it don't work get comm error and return false
ClearCommError(this->hSerial, &this->errors, &this->status);
return false;
}
else
return true;
}
I use this function to send a variable to my Arduino Uno over Serial port like this :
snprintf(Data, sizeof(Data) - 1, "%3.1f", (int)(pf->speedKmh)*1.0);
SP->WriteData(Data, sizeof(Data) - 1); printf("\nData\n");
Some helpful info :
speedkmh is a float
char Data[8] = "";
So my question is : I would like to know exactly what is being sent to the Arduino. At the moment I don't really know if it is sending an array, bits one at a time, if it sends a float etc... Could you help me understand this?
Thanks!
The following line:
snprintf(Data, sizeof(Data) - 1, "%3.1f", (int)(pf->speedKmh)*1.0);
converts your floating point number to a string (an array of characters). Then
SP->WriteData(Data, sizeof(Data) - 1); printf("\nData\n");
sends these characters, one at a time, over the serial port.
BTW, it is not very clear why you convert your float to an integer, then back to a float (by multiplying it by 1.0), then to a string. As I see it, this would have the result of truncating the fractional part of the float, then appending a misleading ".0" to the string (from the ".1f" part of the control string). That is, both 1.0 and 1.4 will be converted to "1.0".

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

How to convert double* data to const char* or QByteArray efficiently

I am trying to use the network programming APIs in Qt in my project. One part of my code requires me to convert double* data to QByteArray or a const char*.
I searched through the stackoverflow questions and could find many people suggesting this code :
QByteArray array(reinterpret_cast<const char*>(data), sizeof(double));
or, for an array of double :
QByteArray::fromRawData(reinterpret_cast<const char*>(data),s*sizeof(double));
When I use them in my function, It does notgive me the desired result. The output seems to be random characters.
Please Suggest an efficient way to implement it in Qt. Thank you very much for your time.
Regards
Alok
If you just need to encode and decode a double into a byte array, this works:
double value = 3.14159275;
// Encode the value into the byte array
QByteArray byteArray(reinterpret_cast<const char*>(&value), sizeof(double));
// Decode the value
double outValue;
// Copy the data from the byte array into the double
memcpy(&outValue, byteArray.data(), sizeof(double));
printf("%f", outValue);
However, that is not the best way to send data over the network, as it will depend on the platform specifics of how the machines encode the double type. I would recommend you look at the QDataStream class, which allows you to do this:
double value = 3.14159275;
// Encode the value into the byte array
QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);
stream << value;
// Decode the value
double outValue;
QDataStream readStream(&byteArray, QIODevice::ReadOnly);
readStream >> outValue;
printf("%f", outValue);
This is now platform independent, and the stream operators make it very convenient and easy to read.
Assuming that you want to create a human readable string:
double d = 3.141459;
QString s = QString::number(d); // method has options for format and precision, see docs
or if you need localization where locale is a QLocale object:
s = locale.toString(d); // method has options for format and precision, see docs
You can easily convert the string into a QByteArray using s.toUtf8() or s.toLatin1() if really necessary. If speed is important there also is:
QByteArray ba = QByteArray::number(d); // method has options for format and precision, see docs

Convert TCHAR* to QString

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)

How to convert TBuf8 to QString

I've tried to convert using the following code:
template< unsigned int size >
static QString
TBuf82QString( const TBuf8< size > &buf )
{
return QString::fromUtf16(
reinterpret_cast<unsigned short*>(
const_cast<TUint8*>(
buf.Ptr() ) ), buf.Length() );
}
But It always returns something like ?????b.
EDIT: Changed code example
Using a template probably isn't a good solution, since it will result in a new instantiation of this block of code within your application binary, for every size of input string which is converted. Since the output type (QString) contains no compile-time constant, this means you end up with code bloat, for no gain.
A better approach would be to leverage the fact that TBuf8<N> inherits from TDesC8:
QString TBuf2QString(const TDesC8 &buf)
{
return QString::fromLocal8Bit(reinterpret_cast<const char *>(buf.Ptr()),
buf.Length());
}
TBuf<16> foo(_L("sometext"));
QString bar = TBuf2QString(foo);
TBuf8 is used for binary data or non-Unicode strings. TBuf16 is used for Unicode strings. TBuf is conditionally compiled and will always be TBuf16 as Symbian OS is natively Unicode.
Try using QString::fromLocal8Bit() with TBuf8::Ptr()

Resources