Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextCodec canEncode, what is the expected behavior?
Forum Updated to NodeBB v4.3 + New Features

QTextCodec canEncode, what is the expected behavior?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 563 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Thuan_FirelightT Offline
    Thuan_FirelightT Offline
    Thuan_Firelight
    wrote on last edited by Thuan_Firelight
    #1

    I am testing a few string conversions for use with an external non-unicode program on Windows using Qt 5.4.1. I have set my windows non-unicode program locale to Japanese and testing the 2 different strings using the code below:

        foreach(const QString& arg, arguments)
        {
            QTextCodec* codec = QTextCodec::codecForLocale();
            QByteArray localizedArg = arg.toLocal8Bit();
            if(QString::fromLocal8Bit(localizedArg) != arg)
            {
                qDebug() << arg << "codec->canEncode" << codec->canEncode(arg) << "QString::fromLocal8Bit(localizedArg) != arg";
            }
            else
            {
                qDebug() << arg << "codec->canEncode" << codec->canEncode(arg);
            }
        }
    

    The variable "arguments" contains the following two strings:

    1. d:/でアヒィン/1.10/Amber/新しいバンク 2.file
    2. d:/1.10/Amber/你你.file

    The first string contains a mixture of English and Japanese characters and the second string contains a mixture of English and Chinese characters.

    Running through the loop above produces the following result in the output window:

    "d:/でアヒィン/1.10/Amber/新しいバンク 2.file" codec->canEncode true
    "d:/1.10/Amber/你你.file" codec->canEncode true QString::fromLocal8Bit(localizedArg) != arg

    If characters are lost or incorrectly converted such that reversing the operation produces a different string, I would expect QTextCodec::canEncode to return false (i.e. if it's going to convert the foreign characters to "?").

    Stepping through the code, I got into QTextCodec::canEncode which the return result is based on state.invalidChars == 0, and just before it, it invoked the QWindowsLocalCodec::convertFromUnicode which doesn't seem to do anything to the ConverterState passed in. Is this correct or a bug?

    aha_1980A 1 Reply Last reply
    0
    • Thuan_FirelightT Thuan_Firelight

      I am testing a few string conversions for use with an external non-unicode program on Windows using Qt 5.4.1. I have set my windows non-unicode program locale to Japanese and testing the 2 different strings using the code below:

          foreach(const QString& arg, arguments)
          {
              QTextCodec* codec = QTextCodec::codecForLocale();
              QByteArray localizedArg = arg.toLocal8Bit();
              if(QString::fromLocal8Bit(localizedArg) != arg)
              {
                  qDebug() << arg << "codec->canEncode" << codec->canEncode(arg) << "QString::fromLocal8Bit(localizedArg) != arg";
              }
              else
              {
                  qDebug() << arg << "codec->canEncode" << codec->canEncode(arg);
              }
          }
      

      The variable "arguments" contains the following two strings:

      1. d:/でアヒィン/1.10/Amber/新しいバンク 2.file
      2. d:/1.10/Amber/你你.file

      The first string contains a mixture of English and Japanese characters and the second string contains a mixture of English and Chinese characters.

      Running through the loop above produces the following result in the output window:

      "d:/でアヒィン/1.10/Amber/新しいバンク 2.file" codec->canEncode true
      "d:/1.10/Amber/你你.file" codec->canEncode true QString::fromLocal8Bit(localizedArg) != arg

      If characters are lost or incorrectly converted such that reversing the operation produces a different string, I would expect QTextCodec::canEncode to return false (i.e. if it's going to convert the foreign characters to "?").

      Stepping through the code, I got into QTextCodec::canEncode which the return result is based on state.invalidChars == 0, and just before it, it invoked the QWindowsLocalCodec::convertFromUnicode which doesn't seem to do anything to the ConverterState passed in. Is this correct or a bug?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Thuan_Firelight please search the forum, I remember a similar question some. I just dont remember the outcome.

      Qt has to stay free or it will die.

      Thuan_FirelightT 1 Reply Last reply
      0
      • aha_1980A aha_1980

        @Thuan_Firelight please search the forum, I remember a similar question some. I just dont remember the outcome.

        Thuan_FirelightT Offline
        Thuan_FirelightT Offline
        Thuan_Firelight
        wrote on last edited by Thuan_Firelight
        #3

        @aha_1980 I did, and the closest one I found was this: https://forum.qt.io/topic/93921/unexpected-result-from-qtextcodec-canencode-qstring/7.

        However, it seems the "solved" response was that "US-ASCII" is not recommended. I am not using "US-ASCII" however. The last response was from the OP and he reckon it is not restricted to "US-ASCII". I wanted to bump the thread, but the forum mechanism recommend I start a new thread as that one is quite old.

        And there is also this bug report: https://bugreports.qt.io/browse/QTBUG-6925, the last comment state it was closed. So I checking what's the expected behavior before I comment further on the bug report to bump it up.

        aha_1980A 1 Reply Last reply
        0
        • Thuan_FirelightT Thuan_Firelight

          @aha_1980 I did, and the closest one I found was this: https://forum.qt.io/topic/93921/unexpected-result-from-qtextcodec-canencode-qstring/7.

          However, it seems the "solved" response was that "US-ASCII" is not recommended. I am not using "US-ASCII" however. The last response was from the OP and he reckon it is not restricted to "US-ASCII". I wanted to bump the thread, but the forum mechanism recommend I start a new thread as that one is quite old.

          And there is also this bug report: https://bugreports.qt.io/browse/QTBUG-6925, the last comment state it was closed. So I checking what's the expected behavior before I comment further on the bug report to bump it up.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Thuan_Firelight

          I would recommend you to place a comment and vote on QTBUG-6925, which was re-opened by the way.

          If you can break down your problem to a minimal, compile and testable example, please attach it there also - it helps debugging and fixing the problem.

          You should of course try that with the latest release (which is 5.12-RC by now) - maybe there is already some improvement for your case.

          Qt has to stay free or it will die.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved