Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reading Config Files in Qt



  • Hi!

    I already have an Qt Project which i am currently working on. I have some lineEdit boxes which has ip address. As of now i tried initializing the lineEdit boxes from constructor part, also user can enter this ip address once gui opens.

    Now i would like to try with config files. Basically my idea here is I want to give two ip address in config file. Then in the constructor part i would like to read this config file and set this ip to lineEdit objects.

    I can also do this by ui->lineEdit->setText(""), but i am just trying to do via config file. I am trying some various methods.

    I went to this documentation(https://doc.qt.io/qt-5/qsettings.html) but i didnt get full clarity.

    I am using windows machine. So where do I need to keep my config file. Should I keep in the same project directory?

    Also How to read data from config file. Is there any preferred way of reading data from config file or normal parsing?

    There is no particular reason why i am trying to use config file, i just wanted to try.


  • Lifetime Qt Champion

    @aravmadd said in Reading Config Files in Qt:

    So where do I need to keep my config file.

    You should store it in a location where the user has write access to - see QStandardLocation

    Also How to read data from config file. Is there any preferred way of reading data from config file or normal parsing?

    Use QSettings


  • Lifetime Qt Champion

    @aravmadd said in Reading Config Files in Qt:

    Should I keep in the same project directory?

    No, that folder is not always writable by user. You actually don't have to worry about settings file location, QSettings takes care. It is explained in the documentation (the link you posted). On windows you need to specify IniFormat if you want to store settings in a file, else they are stored in registry. In case you set IniFormat the file will be located in (from documentation):

    On Windows, the following files are used:
    
        FOLDERID_RoamingAppData\MySoft\Star Runner.ini
        FOLDERID_RoamingAppData\MySoft.ini
        FOLDERID_ProgramData\MySoft\Star Runner.ini
        FOLDERID_ProgramData\MySoft.ini
    

    How to read settings is also explained in documentation (with examples), please take time to read it.



  • @jsulm Thanks after reading documentation again, i was able to use QSettings
    For anyone who needs some idea on using config files below code snippet might help you.

     ui->pushButton->setVisible(false);
     QSettings settings("../config/settings.ini",QSettings::IniFormat);
     QStringList keys = settings.allKeys();
    
     for (auto i=0;i<keys.size();i++)
     {
         configvalues.push_back(settings.value(keys[i]).toString());
     }
    
     Channel = configvalues[0].toInt() ;
     auto my_Ip = configvalues[1];
     auto hostIp = configvalues[2];
     auto iterations = configvalues[3];
     Port = configvalues[0].toInt();