6

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;
}
4

1 に答える 1

7

QStringをstd::wstringwchar_t配列の両方に変換する例を次に示します。

#include <iostream>
#include <QtCore/QString>

using namespace std;

int main ()
{
    // Create QT string.
    QString qstr = "Hello World";

    // Convert it into standard wstring.
    std::wstring str = qstr.toStdWString ();

    // Get the wchar_t pointer to the standard string.
    const wchar_t *p = str.c_str ();

    // Output results...
    wcout << str << endl;
    wcout << p << endl;

    // Example of converting to C array of wchar_t.
    wchar_t array[qstr.size () + 1];
    int length = qstr.toWCharArray (array);
    if (length < 0 || length >= sizeof(array) / sizeof (array[0]))
    {
        cerr << "Conversion failed..." << endl;
        return 1;
    }

    array[length] = '\0'; // Manually put terminating character.
    wcout << array << endl; // Output...
}

QStringを配列に変換するとエラーが発生しやすくなり、Unicode文字列を出力するには、 std::coutの代わりにstd::wcoutを使用する必要があることに注意してください。これは同じ出力ストリームですがwchar_t用です。

于 2010-09-16T13:13:14.787 に答える