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 Config Files in Qt
QtWS25 Last Chance

Reading Config Files in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.12.2config fileqsettings
4 Posts 3 Posters 4.1k 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.
  • A Offline
    A Offline
    aravmadd
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • A aravmadd

        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.

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

        @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.

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

        A 1 Reply Last reply
        4
        • jsulmJ jsulm

          @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.

          A Offline
          A Offline
          aravmadd
          wrote on last edited by
          #4

          @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();
          
          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