Results 1 to 5 of 5

Thread: QString::replace difference in behavior

  1. #1
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QString::replace difference in behavior

    Hallo there,

    I have lost sooo much time trying to figure this out and it doesn't make much sense for me!
    The problem is that QString::replace behaves different under Qt5-Windows compared to Qt4-Windows or Qt5-Linux!

    Using:
    ============
    Windows 10 Pro
    21H1
    19043.1165
    Windows Feature Experience Pack 120.2212.3530.0
    ============
    Linux:
    NAME="openSUSE Tumbleweed"
    # VERSION="20210912"
    ID="opensuse-tumbleweed"
    ID_LIKE="opensuse suse"
    VERSION_ID="20210912"
    PRETTY_NAME="openSUSE Tumbleweed"
    ============
    Qt 4.8.7 (only on Windows)
    Qt 5.15.2 (Windows and Linux)
    MSVC 2019 compiler under Windows
    ============

    I just want to change some "special" characters with others. The code I use should be portable, (at least IMHO) there is no magic in it at all.
    But here the code (it is not minimal but runs alone) first to better understand what I want:

    Qt Code:
    1. #include <QDebug>
    2.  
    3. // -----------------------------------------------------------------------------
    4. // -----------------------------------------------------------------------------
    5. void checkStrings(const QString& str1, const QString& str2)
    6. {
    7. if (str1 == str2) {
    8. qDebug() << "=== GOOD ===";
    9. } else {
    10. qDebug() << "### BAD ###";
    11. }
    12. qDebug() << "str1:" << str1;
    13. qDebug() << "str2:" << str2;
    14. }
    15.  
    16. // -----------------------------------------------------------------------------
    17. // -----------------------------------------------------------------------------
    18. void replaceStrings()
    19. {
    20. qDebug() << "\t----> ENTERING: " << Q_FUNC_INFO;
    21. const QString src = "[aabbAABBaabbAABB]";
    22. QString dst = "";
    23. QString tmp = "";
    24.  
    25. tmp = src;
    26. tmp.replace("a", "-");
    27. dst = "[--bbAABB--bbAABB]";
    28. checkStrings(tmp, dst);
    29. }
    30.  
    31. // -----------------------------------------------------------------------------
    32. // -----------------------------------------------------------------------------
    33. void replaceUmlauts()
    34. {
    35. qDebug() << "\t----> ENTERING: " << Q_FUNC_INFO;
    36. const QString src = "[ää,ÄÄ,öö,ÖÖ,üü,ÜÜ]";
    37. QString dst = "";
    38. QString tmp = "";
    39.  
    40. tmp = src;
    41. tmp.replace("ä", "-");
    42. // tmp.replace(QString("ä"), QString("-")); // doesn't matter if we do it this or that way...
    43. dst = "[--,ÄÄ,öö,ÖÖ,üü,ÜÜ]";
    44. checkStrings(tmp, dst);
    45. }
    46.  
    47. // -----------------------------------------------------------------------------
    48. // -----------------------------------------------------------------------------
    49. int main(int argc, char *argv[])
    50. {
    51. Q_UNUSED(argc);
    52. Q_UNUSED(argv);
    53.  
    54. replaceStrings();
    55. replaceUmlauts();
    56.  
    57. return 0;
    58. }
    59.  
    60. // EOF
    To copy to clipboard, switch view to plain text mode 

    The most accurate answer to that should be and is the one under Linux:
    Linux (Qt5) output:

    Qt Code:
    1. ----> ENTERING: void replaceStrings()
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void replaceUmlauts()
    6. === GOOD ===
    7. str1: "[--,ÄÄ,öö,ÖÖ,üü,ÜÜ,--,ÄÄ,öö,ÖÖ,üü,ÜÜ]"
    8. str2: "[--,ÄÄ,öö,ÖÖ,üü,ÜÜ,--,ÄÄ,öö,ÖÖ,üü,ÜÜ]"
    To copy to clipboard, switch view to plain text mode 

    The windows outputs are not giving everything correct back because the console uses codepage 850. But that is OK!
    Windows 10 - Qt4 output:

    Qt Code:
    1. ----> ENTERING: void __cdecl replaceStrings(void)
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void __cdecl replaceUmlauts(void)
    6. === GOOD ===
    7. str1: "[--,??,÷÷,ÍÍ,³³,??,--,??,÷÷,ÍÍ,³³,??]"
    8. str2: "[--,??,÷÷,ÍÍ,³³,??,--,??,÷÷,ÍÍ,³³,??]"
    To copy to clipboard, switch view to plain text mode 

    Windows 10 - Qt5 output:

    Qt Code:
    1. ----> ENTERING: void __cdecl replaceStrings(void)
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void __cdecl replaceUmlauts(void)
    6. ### BAD ###
    7. str1: "[--,--,--,--,--,--,--,--,--,--,--,--]"
    8. str2: "[--,??,??,??,??,??,--,??,??,??,??,??]"
    To copy to clipboard, switch view to plain text mode 

    The problem is that the second compare (line 6) in "replaceUmlauts()" results to "### BAD ###".
    WHYYYYYYYYYYYY is that so???!?!?!?!??!

    Just trying to change:
    the "a"s to "-"s in the first source QString: "[aa,bb,AA,BB,aa,bb,AA,BB]"
    and the "ä"s to "-"s again in the second one "[ää,ÄÄ,öö,ÖÖ,üü,ÜÜ,ää,ÄÄ,öö,ÖÖ, üü,ÜÜ]".

    Thanks for ANY help on that in advance!
    Last edited by freeman_w; 15th September 2021 at 13:32.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,018
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QString::replace difference in behavior

    Try converting your strings to Latin1 or utf-8 before doing the replace on the resulting QByteArray. Don't know if this will help. Character conversions and codepage issues can be very tricky.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString::replace difference in behavior

    Thanks for the answer d_stranz.

    I don't want to make any change to the code because Qt4 under Windows DOES work as intended and also Qt5 under Linux. And I am not making anything fancy there. Just using the "QString::replace()" function and I expect it to behave like the documentation writes.
    I just want to understand why Qt5-Windows behaves differently. Any clues?

    BTW, the source file is ALWAYS in UTF8 (will add this to the first post, forgot about that).

    Quote Originally Posted by d_stranz View Post
    Try converting your strings to Latin1 or utf-8 before doing the replace on the resulting QByteArray. Don't know if this will help. Character conversions and codepage issues can be very tricky.

  4. #4
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString::replace difference in behavior

    OK, the problem seems to be the MSCV 2019 compiler.
    I tried to use another kit today and added the MinGW 8.1.0 compiler along with Qt5 for windows. Result:

    Windows 10 - Qt5 - MinGW output:
    Qt Code:
    1. ----> ENTERING: void replaceStrings()
    2. === GOOD ===
    3. str1: "[--,bb,AA,BB,--,bb,AA,BB]"
    4. str2: "[--,bb,AA,BB,--,bb,AA,BB]"
    5. ----> ENTERING: void replaceUmlauts()
    6. === GOOD ===
    7. str1: "[--,??,÷÷,ÍÍ,³³,??,--,??,÷÷,ÍÍ,³³,??]"
    8. str2: "[--,??,÷÷,ÍÍ,³³,??,--,??,÷÷,ÍÍ,³³,??]"
    To copy to clipboard, switch view to plain text mode 

    So after really finding out who the bad guy was by trying to make a small program for you here, I also found out how to make him work as wanted.
    Just add in your profile:

    Qt Code:
    1. QMAKE_CXXFLAGS += -source-charset:utf-8
    2. QMAKE_CXXFLAGS += -execution-charset:utf-8
    To copy to clipboard, switch view to plain text mode 

    somewhere, along with any conditions you need and it works also good with MSCV 2019.
    I hope this saves some time on everyone who encounters it!

    Thanks and till next time!
    freeman_w
    Last edited by freeman_w; 16th September 2021 at 06:45.

  5. The following user says thank you to freeman_w for this useful post:

    d_stranz (16th September 2021)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,018
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QString::replace difference in behavior

    QMAKE_CXXFLAGS += -source-charset:utf-8
    QMAKE_CXXFLAGS += -execution-charset:utf-8
    Good detective work. Surprising to me that the compiler (or maybe the IDE) would let you type characters with umlauts as part of your source code and then mangle them when it built the code.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Qt won't recognize QString::replace function.
    By Warumato in forum Newbie
    Replies: 3
    Last Post: 19th March 2020, 17:41
  2. QString unicode replace
    By trallallero in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2012, 14:57
  3. qstring replace backslash by forward-slashes
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 12:07
  4. [solved]QString.replace()
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2011, 09:15
  5. QString - find and replace text
    By graciano in forum Newbie
    Replies: 3
    Last Post: 24th January 2009, 20:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.