Help to port c# to Qt c++
-
Hello, i m beginner in Qt c++ and i try to port a c# programm to Qt c++.
I don't know how to do that:in c#:
byte[] t = System.Text.Encoding.UTF8.GetBytes(trame); // trame is a stringin Qt c++:
?Can anyone help me please?
char[] t= QString(trame).toUtf8().constData() -
Hi,
Since you are likely going to use t somewhere else, rather:
QByteArray trameBytes = QString(trame).toUtf8(); char[] t = trameBytes.constData();otherwise you might be pointing to a buffer that can get invalidated.
-
Ok, thank you for yours fast answers
but when i try :
QByteArray trameBytes = QString(trame).toUtf8();
char t[] = trameBytes.constData();i can't compile:
initializer fails to determine size of 't"
array must be initialised wit a brace-enclosed initialiseri don't know how to determine the size because 'trame' is variable
-
Depending on what you are going to do with it, use a
const char *.In fact, what do you need t for ?
-
Hi
The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
Why not use constData() directly ?
clientSocket->write(trameBytes.constData(), xxxor
const char* t = trameBytes.constData();Also if socket is Qt version, i think it can take ByteArray directly.
-
No need for all that stuff then.
clientSocket->write(frameBytes);is enough.
-
Hi
The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
Why not use constData() directly ?
clientSocket->write(trameBytes.constData(), xxxor
const char* t = trameBytes.constData();Also if socket is Qt version, i think it can take ByteArray directly.
@mrjj said in Help to port c# to Qt c++:
The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
You're correct, and our fellows fell into that one head first. :)
char t[]is a valid construction only in two cases:- If the size of
tcan be determined at compile-time, e.g.char t[] = { 'a', 'b' };. - It's a formal parameter for a function, then
char t[]equates tochar * const t.
Note: When passing multidimensional arrays as formal parameters only the first dimension can be left without explicitly specifying the size.
- If the size of
-
@mrjj said in Help to port c# to Qt c++:
The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
You're correct, and our fellows fell into that one head first. :)
char t[]is a valid construction only in two cases:- If the size of
tcan be determined at compile-time, e.g.char t[] = { 'a', 'b' };. - It's a formal parameter for a function, then
char t[]equates tochar * const t.
Note: When passing multidimensional arrays as formal parameters only the first dimension can be left without explicitly specifying the size.
@kshegunov said in Help to port c# to Qt c++:
You're correct, and our fellows fell into that one head first. :)
yep, my fault. Answered too fast.
- If the size of
-
@kshegunov said in Help to port c# to Qt c++:
You're correct, and our fellows fell into that one head first. :)
yep, my fault. Answered too fast.
@raven-worx, I wasn't looking to place blame, I just found it somewhat funny (no offence). :D
-
@raven-worx, I wasn't looking to place blame, I just found it somewhat funny (no offence). :D
@kshegunov
i didn't take it as such anyway.
No piece of code exists without bugs ;) -
Again: there's no need to call constData. QTcpSocket has a write method that takes a QByteArray.
-
@SGaist said in Help to port c# to Qt c++:
No need for all that stuff then.
clientSocket->write(frameBytes);is enough.
As a general rule of thumb, when you use Qt, use QString and QByteArray as early as you can and use char*-arrays as late as possible. If you create a string with "", create a QString: QString("a string you know compile time"). If you get [] array data from outside Qt program, convert it as soon as you get it. Most Qt classes/functions which can use char* directly have that possibility only for convenience: for example QTcpSocket and other classes which communicate with outside world take and give QByteArrays. Low level stuff or c++ standard library stuff is of course sometimes necessary or preferable (there might be several good reasons), but don't use it unless you have a specific reason. See docs for QString, QByteArray and QIODevice. They are all important basic classes in Qt programming. And of course the container classes like QList.
-
@SGaist said in Help to port c# to Qt c++:
No need for all that stuff then.
clientSocket->write(frameBytes);is enough.
As a general rule of thumb, when you use Qt, use QString and QByteArray as early as you can and use char*-arrays as late as possible. If you create a string with "", create a QString: QString("a string you know compile time"). If you get [] array data from outside Qt program, convert it as soon as you get it. Most Qt classes/functions which can use char* directly have that possibility only for convenience: for example QTcpSocket and other classes which communicate with outside world take and give QByteArrays. Low level stuff or c++ standard library stuff is of course sometimes necessary or preferable (there might be several good reasons), but don't use it unless you have a specific reason. See docs for QString, QByteArray and QIODevice. They are all important basic classes in Qt programming. And of course the container classes like QList.
With the minor note you should use
QStringLiteral("some literal")orQLatin1Literal("latin 1 literal")to enablemocto do a bit of magic and optimize some constructor calls.