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. error read with useing QSettings
Qt 6.11 is out! See what's new in the release blog

error read with useing QSettings

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 2.6k 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.
  • M Offline
    M Offline
    MLGDN
    wrote on last edited by
    #1

    I use follow code to read ini file:

        QSettings settings(fileName, QSettings::IniFormat);
        settings.setIniCodec(QTextCodec::codecForName("utf-8"));
        settings.value("/Group/name", "default").toString();
    

    the value in the file without comma is right, but it goes error when it contains , and the strange is if comma is under Chinese input method not English it is right. How to solve this problem?

    jsulmJ joeQJ T 3 Replies Last reply
    0
    • M MLGDN

      I use follow code to read ini file:

          QSettings settings(fileName, QSettings::IniFormat);
          settings.setIniCodec(QTextCodec::codecForName("utf-8"));
          settings.value("/Group/name", "default").toString();
      

      the value in the file without comma is right, but it goes error when it contains , and the strange is if comma is under Chinese input method not English it is right. How to solve this problem?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MLGDN Could you please explain better what the problem is?
      Can you show the ini file you're trying to read?
      And what error do you get?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M MLGDN

        I use follow code to read ini file:

            QSettings settings(fileName, QSettings::IniFormat);
            settings.setIniCodec(QTextCodec::codecForName("utf-8"));
            settings.value("/Group/name", "default").toString();
        

        the value in the file without comma is right, but it goes error when it contains , and the strange is if comma is under Chinese input method not English it is right. How to solve this problem?

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @MLGDN Hi,friend. Welcome.

        I think you should not set utf-8. In Qt5 is not to set, just like below:

        
            QSettings settings("test.ini", QSettings::IniFormat);
            //settings.setIniCodec(QTextCodec::codecForName("utf-8"));
            settings.setValue("key1","IamEnglish");
            settings.setValue("key2","I,am,English");
            settings.setValue("key3","我,是,中文");
        
            qDebug() << settings.value("key1","Default").toString();
            qDebug() << settings.value("key2","Default").toString();
            qDebug() << settings.value("key3","Default").toString();
        
        

        and the ini file:

        [General]
        key1=IamEnglish
        key2="I,am,English"
        key3=\x6211\xff0c\x662f\xff0c\x4e2d\x6587
        
        

        In the ini file, Qt used \x to encode Chinese.

        Just do it!

        M 1 Reply Last reply
        0
        • M MLGDN

          I use follow code to read ini file:

              QSettings settings(fileName, QSettings::IniFormat);
              settings.setIniCodec(QTextCodec::codecForName("utf-8"));
              settings.value("/Group/name", "default").toString();
          

          the value in the file without comma is right, but it goes error when it contains , and the strange is if comma is under Chinese input method not English it is right. How to solve this problem?

          T Offline
          T Offline
          Tirupathi Korla
          wrote on last edited by
          #4

          @MLGDN
          When there is a comma in value of ini file and if you read the value with a key, it will return a list.
          settings.value("/Group/name", "default").toStringList(); will return an array of strings.
          for eg. if you have

          testKey=a,b,c,d in ini file and if you fetch value

          qDebug() << settings.value("testKey","default").toStringList();

          you will get
          ("a", "b", "c", "d")

          M 1 Reply Last reply
          2
          • T Tirupathi Korla

            @MLGDN
            When there is a comma in value of ini file and if you read the value with a key, it will return a list.
            settings.value("/Group/name", "default").toStringList(); will return an array of strings.
            for eg. if you have

            testKey=a,b,c,d in ini file and if you fetch value

            qDebug() << settings.value("testKey","default").toStringList();

            you will get
            ("a", "b", "c", "d")

            M Offline
            M Offline
            MLGDN
            wrote on last edited by
            #5

            @Tirupathi-Korla
            thank you , it works

            1 Reply Last reply
            0
            • joeQJ joeQ

              @MLGDN Hi,friend. Welcome.

              I think you should not set utf-8. In Qt5 is not to set, just like below:

              
                  QSettings settings("test.ini", QSettings::IniFormat);
                  //settings.setIniCodec(QTextCodec::codecForName("utf-8"));
                  settings.setValue("key1","IamEnglish");
                  settings.setValue("key2","I,am,English");
                  settings.setValue("key3","我,是,中文");
              
                  qDebug() << settings.value("key1","Default").toString();
                  qDebug() << settings.value("key2","Default").toString();
                  qDebug() << settings.value("key3","Default").toString();
              
              

              and the ini file:

              [General]
              key1=IamEnglish
              key2="I,am,English"
              key3=\x6211\xff0c\x662f\xff0c\x4e2d\x6587
              
              

              In the ini file, Qt used \x to encode Chinese.

              M Offline
              M Offline
              MLGDN
              wrote on last edited by
              #6

              @joeQ
              my file is utf-8 format, this way not works

              1 Reply Last reply
              1

              • Login

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