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. Reading .ini files: problems with argument

Reading .ini files: problems with argument

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 1.8k 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.
  • F Faruq

    Hi people,

    For today, I am exploring how to read on .ini files . Since I recently start to learn how to read Qt documentation, I followed closely to the syntax/argument required. The problem lies on the AREA of ATTENTION 1 where I start to use const char * at the argument "c". AREA OF ATTENTION 2 is the snippet I managed to find online and it works (Managed to extract the data I want). The reason why I dont wish to use "SN1" is because I would like "c" to act as the argument to AREA OF ATTENTION 1 line. (for flexibility)

    std::string search_num = "SN1";
    const char * c =  search_num.c_str();                              //conversion from std::string to const char *
    QSettings settings("C:/Users/Common/Desktop/PopupText/PopupText/textOption.ini", QSettings::IniFormat);
    settings.beginGroup("warnings");
     //AREA OF ATTENTION 1 ->  QString param = settings.value(c.c_str()).toString();             
     //AREA OF ATTENTION 2  ->//QString param = settings.value("SN1").toString();                                 //snippet from online where "SN1" suggest const char*. Using this method works but not ideal due to manual keying.
    settings.endGroup();
    
        qDebug() << param;  //result in:  " "
        qDebug() << c;       // result in : SN1
    

    Why does this happen, and how do I resolve AREA OF ATTENTION 1 since it doesn't seems to read even though I have input the right argument type?

    Thank you and looking forward for your help :)

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

    @Faruq Sorry I don't understand your description!
    Why do you want to use char* in QSettings::value? There is exactly 0 reason to do so as it accepts QString.
    What is this:

    //AREA OF ATTENTION 1 ->  QString param = settings.value(c.c_str()).toString();             
     //AREA OF ATTENTION 2  ->//QString param = settings.value("SN1").toString();
    

    ?
    What is "AREA OF ATTENTION"?
    Can you show the content of your INI file and explain what you want to read from it?

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

    F 1 Reply Last reply
    3
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi @Faruq,

      So am I right you want something like this?

      QString key = "SN1";
      QSettings settings("C:/Users/Common/Desktop/PopupText/PopupText/textOption.ini", QSettings::IniFormat);
      settings.beginGroup("warnings");
      QString param = settings.value(key).toString();             
      settings.endGroup();
      

      Qt has to stay free or it will die.

      F 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Faruq Sorry I don't understand your description!
        Why do you want to use char* in QSettings::value? There is exactly 0 reason to do so as it accepts QString.
        What is this:

        //AREA OF ATTENTION 1 ->  QString param = settings.value(c.c_str()).toString();             
         //AREA OF ATTENTION 2  ->//QString param = settings.value("SN1").toString();
        

        ?
        What is "AREA OF ATTENTION"?
        Can you show the content of your INI file and explain what you want to read from it?

        F Offline
        F Offline
        Faruq
        wrote on last edited by
        #4

        @jsulm hi, thank you for your time. I did try Qstring but it give me a blank output(""). Why char? Because I am following closely with an example snippet. I also tried removing .tostring() but qDebug() shows that it is QVariant. Only with char constant (strictly typed with " - words-" then will it appear)

        As to what is in .ini file, it's nothing much. It's just a tag

        Wordings.ini
        [warnings]
        SN1 = hello
        SN2 = hi
        SN3 = so on and so fourth.

        That is why I seek help :)

        jsulmJ 1 Reply Last reply
        0
        • F Faruq

          @jsulm hi, thank you for your time. I did try Qstring but it give me a blank output(""). Why char? Because I am following closely with an example snippet. I also tried removing .tostring() but qDebug() shows that it is QVariant. Only with char constant (strictly typed with " - words-" then will it appear)

          As to what is in .ini file, it's nothing much. It's just a tag

          Wordings.ini
          [warnings]
          SN1 = hello
          SN2 = hi
          SN3 = so on and so fourth.

          That is why I seek help :)

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

          @Faruq In the code you posted you're using std::string not QString.
          Did you try what @aha_1980 posted?

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

          F 1 Reply Last reply
          0
          • aha_1980A aha_1980

            Hi @Faruq,

            So am I right you want something like this?

            QString key = "SN1";
            QSettings settings("C:/Users/Common/Desktop/PopupText/PopupText/textOption.ini", QSettings::IniFormat);
            settings.beginGroup("warnings");
            QString param = settings.value(key).toString();             
            settings.endGroup();
            
            F Offline
            F Offline
            Faruq
            wrote on last edited by
            #6

            @aha_1980 hi. Thank you for your reply. Yes. I did that but it doesn't output anything from .ini file. It only show the output if I replace "key" with ("SN1").

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Faruq In the code you posted you're using std::string not QString.
              Did you try what @aha_1980 posted?

              F Offline
              F Offline
              Faruq
              wrote on last edited by
              #7

              @jsulm hmm, I tried converting from std::string into Qstring too. But it doesn't work. Is it something to do with a conversion bug?

              For Qstring conversion, I used Qstring example = std_str.tostring() .

              jsulmJ 1 Reply Last reply
              0
              • F Faruq

                @jsulm hmm, I tried converting from std::string into Qstring too. But it doesn't work. Is it something to do with a conversion bug?

                For Qstring conversion, I used Qstring example = std_str.tostring() .

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

                @Faruq said in Reading .ini files: problems with argument:

                hmm, I tried converting from std::string into Qstring too

                Why do you convert std::string to QString at all?! Why not simply do it like @aha_1980 suggested? There is no need for std::string...
                "conversion bug" - what conversion bug?
                Also did you make sure that the path to the file is valid?

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

                F 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Faruq said in Reading .ini files: problems with argument:

                  hmm, I tried converting from std::string into Qstring too

                  Why do you convert std::string to QString at all?! Why not simply do it like @aha_1980 suggested? There is no need for std::string...
                  "conversion bug" - what conversion bug?
                  Also did you make sure that the path to the file is valid?

                  F Offline
                  F Offline
                  Faruq
                  wrote on last edited by Faruq
                  #9

                  @jsulm I am using such conversion as the value being passed into the function is a std::string type. Thus I really require it to be as such.

                  Yes. The directory is correct because if its not, I would not receive any data at all with the hard code "SN1".

                  jsulmJ 1 Reply Last reply
                  0
                  • F Faruq

                    @jsulm I am using such conversion as the value being passed into the function is a std::string type. Thus I really require it to be as such.

                    Yes. The directory is correct because if its not, I would not receive any data at all with the hard code "SN1".

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

                    @Faruq The "conversion bug" is in your code.
                    I just tried this:

                    std::string tmp = "SN1";
                    QString key = tmp.c_str();
                    QSettings settings("C:/Users/q295085/Desktop/some.ini", QSettings::IniFormat);
                    settings.beginGroup("warnings");
                    QString param = settings.value(key).toString();
                    qDebug() << param;
                    settings.endGroup();
                    

                    And it works.

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

                    aha_1980A F 2 Replies Last reply
                    2
                    • jsulmJ jsulm

                      @Faruq The "conversion bug" is in your code.
                      I just tried this:

                      std::string tmp = "SN1";
                      QString key = tmp.c_str();
                      QSettings settings("C:/Users/q295085/Desktop/some.ini", QSettings::IniFormat);
                      settings.beginGroup("warnings");
                      QString param = settings.value(key).toString();
                      qDebug() << param;
                      settings.endGroup();
                      

                      And it works.

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

                      There is also QString::fromStdString, which avoids the c_str().

                      Please note: std::string is, in contrast to QString, no unicode string, so you have to convert your data carefully if the std::string contains non-ASCII characters. E.g. fromStdString() assumes the data in UTF-8.

                      Qt has to stay free or it will die.

                      F 1 Reply Last reply
                      2
                      • jsulmJ jsulm

                        @Faruq The "conversion bug" is in your code.
                        I just tried this:

                        std::string tmp = "SN1";
                        QString key = tmp.c_str();
                        QSettings settings("C:/Users/q295085/Desktop/some.ini", QSettings::IniFormat);
                        settings.beginGroup("warnings");
                        QString param = settings.value(key).toString();
                        qDebug() << param;
                        settings.endGroup();
                        

                        And it works.

                        F Offline
                        F Offline
                        Faruq
                        wrote on last edited by
                        #12

                        @jsulm alright! I have yet to check and replace the constant char into Qstring. Will test it out soon enough and will ask qn if I'm still in doubt. Thank you jsulm :)

                        1 Reply Last reply
                        0
                        • aha_1980A aha_1980

                          There is also QString::fromStdString, which avoids the c_str().

                          Please note: std::string is, in contrast to QString, no unicode string, so you have to convert your data carefully if the std::string contains non-ASCII characters. E.g. fromStdString() assumes the data in UTF-8.

                          F Offline
                          F Offline
                          Faruq
                          wrote on last edited by
                          #13

                          @aha_1980 ah I have yet to try that. I will take note, avoid such conversion method and let you know if it works :) thank you for your help.

                          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