QChar を wchar_t に変換する必要があります
私は次のことを試しました:
#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
QString mystring = "Hello World\n";
wchar_t myArray[mystring.size()];
for (int x=0; x<mystring.size(); x++)
{
myArray[x] = mystring.at(x).toLatin1();
cout << mystring.at(x).toLatin1(); // checks the char at index x (fine)
}
cout << "myArray : " << myArray << "\n"; // doesn't give me correct value
return 0;
}
ああ、誰かが .toWCharArray(wchar_t* array) 関数の使用を提案する前に、私はそれを試しましたが、基本的に上記と同じことを行い、必要な文字を転送しません。
あなたが私を信じていない場合、以下はそのコードです:
#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
QString mystring = "Hello World\n";
cout << mystring.toLatin1().data();
wchar_t mywcharArray[mystring.size()];
cout << "Mystring size : " << mystring.size() << "\n";
int length = -1;
length = mystring.toWCharArray(mywcharArray);
cout << "length : " << length;
cout << mywcharArray;
return 0;
}
助けてください、私はこの単純な問題に何日も悩まされてきました。理想的には wchar_t をまったく使用したくないのですが、残念ながら、シリアル RS232 コマンドを使用してポンプを制御するには、サードパーティ関数でこの型へのポインターが必要です。
ありがとう。
編集: このコードを実行するには、QT ライブラリが必要です。これらは、QT クリエーターをダウンロードして取得できます。コンソールで出力を取得するには、コマンド「CONFIG += console」を .pro ファイルに追加する必要があります ( QT 作成者) または netbeans プロジェクトを使用している場合は、プロパティの下のカスタム定義に。
編集:
以下の Vlad の正しい回答に感謝します。
これは同じことを行うための更新されたコードですが、char メソッドによる転送を使用し、null 終端を追加することを忘れないでください。
#include <cstdlib>
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
QString mystring = "Hello World\n";
wchar_t myArray[mystring.size()];
for (int x=0; x<mystring.size(); x++)
{
myArray[x] = (wchar_t)mystring.at(x).toLatin1();
cout << mystring.at(x).toLatin1();
}
myArray[mystring.size()-1] = '\0'; // Add null character to end of wchar array
wcout << "myArray : " << myArray << "\n"; // use wcout to output wchar_t's
return 0;
}