Qt convert unicode entities

后端 未结 3 1389
日久生厌
日久生厌 2020-12-20 18:02

In QT 5.4 and C++ I try to decode a string that has unicode entities.

I have this QString:

QString string = \"file\\u00d6\\u00c7\\u015e\\u0130\\u011e         


        
相关标签:
3条回答
  • 2020-12-20 18:30

    I have just tested this code:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QString s = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
        qDebug() << s.length();  //Outputs: 22
        qDebug() << s;           //Outputs: fileÖÇŞİĞÜİçöşiğüı.txt
        return a.exec();
    }
    

    This is with Qt 5.4 on ubuntu, so it looks like your problem is with some OS only.

    0 讨论(0)
  • 2020-12-20 18:38

    Qt provides a macro called QStringLiteral for handling string literals correctly.

    Here's a full working example:

    #include <QString>
    #include <QDebug>
    
    int main(void) {
       QString string = QStringLiteral("file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt");
       qDebug() << string;
    
       return 0;
    }
    

    As mentioned in the above comments, you do need to print to a console that supports these characters for this to work.

    0 讨论(0)
  • 2020-12-20 18:47
    #include <QTextDocument>
    
    QTextDocument doc;
    
    QString string = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
    doc.setHtml(string);                   // to convert entities to text
    QString result = doc.toPlainText();    // result = "fileÖÇŞİĞÜİçöşiğüı.txt"
    

    NOT USEFUL if you have a CONSOLE app QTextDocument needs the GUI module.

    0 讨论(0)
提交回复
热议问题