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. QString global variable and coding problem
Forum Update on Monday, May 27th 2025

QString global variable and coding problem

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.0k 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.
  • H Offline
    H Offline
    Hostel
    wrote on 20 Jul 2012, 01:17 last edited by
    #1

    I have a problem with QString which is a global variable. Coding of this variable is wrong. File is in UTF8. Here is example code:
    @
    #include <QtCore/QCoreApplication>
    #include <QTextCodec>
    #include <QDebug>

    QString global( "ąęśćź" );

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    
    QString inside( "ąęśćź" );
    
    qDebug() << "Global: " << global; // print Global:  "ąęśćź"  - it's wrong
    qDebug() << "Inside: " << inside; // print Inside:  "ąęśćź" - good
    
    return a.exec&#40;&#41;;
    

    }
    @
    How to solve it?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on 20 Jul 2012, 06:30 last edited by
      #2

      Hello,

      How about utf-8 BOM.
      if you select "add if encoding is UTF-8" (on windows), it may work correctly.

      dmcr

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sidewinder
        wrote on 20 Jul 2012, 10:01 last edited by
        #3

        Your global QString is created before you set codec for c strings to UTF8 so default Latin1 is used. Hence text corruption.

        "Never memorize what you can look up in books."
        Albert Einstein

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hostel
          wrote on 20 Jul 2012, 15:22 last edited by
          #4

          sidewinder your are right. To print correct global string this is required:
          @
          global.toLatin1()
          @

          1 Reply Last reply
          0

          1/4

          20 Jul 2012, 01:17

          • Login

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