Question

Je dois convertir un QChar à un wchar_t

J'ai essayé ce qui suit:

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

Oh, et avant que quelqu'un suggère d'utiliser la .toWCharArray (wchar_t * tableau) fonction, je l'ai essayé et ça fait essentiellement la même chose que ci-dessus et ne transfère pas les caractères comme il se doit.

Voici le code pour que si vous ne me croyez pas:

#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;

}

S'il vous plaît aider, je suis à ce problème simple jours. Je voudrais idéalement comme de ne pas utiliser wchar_t est du tout, mais malheureusement un pointeur sur ce type est nécessaire dans une fonction tierce partie pour contrôler une pompe à l'aide des commandes série RS232.

Merci.

EDIT: Pour exécuter ce code, vous aurez besoin des bibliothèques QT, vous pouvez obtenir en téléchargeant ce créateur QT et pour obtenir la sortie de la console, vous devrez ajouter la commande « + = console CONFIG » au .pro fichier (dans le créateur QT) ou aux définitions personnalisées dans les propriétés si vous utilisez un projet NetBeans.

EDIT:

Merci à Vlad ci-dessous pour sa réponse correcte:

Voici le code mis à jour pour faire la même chose mais en utilisant un char transfert par la méthode char et en se rappelant d'ajouter la terminaison 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;
}
Était-ce utile?

La solution

Voici un exemple de conversion QString à la fois std :: wstring et wchar_t tableau:

#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...
}

S'il vous plaît noter que la conversion QString dans un tableau est plus sujette aux erreurs et à la sortie chaîne unicode, vous devez utiliser std :: wcout au lieu de std :: cout, qui est le même flux de sortie mais pour wchar_t .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top