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. Read an .ini file and then launch commands from a console application ?
Forum Updated to NodeBB v4.3 + New Features

Read an .ini file and then launch commands from a console application ?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.4k Views 2 Watching
  • 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.
  • W Offline
    W Offline
    wowy
    wrote on last edited by
    #1

    Hi,
    I am trying to make a qt console application which could read a .ini file and then run commands with the data from this file.

    My ini look like this :
    [IP]
    MAC=61:55:60:62:4e:bc
    ADDRESS=10.2.29.255
    NETMASK=255.255.0.0
    GATEWAY=10.2.1.1
    DNS=10.2.2.110
    DHCP=0

    What i would like to know how to too would be how to read the data in this file and how i could use them to launch command like :
    ifconfig lo 127.0.0.1 netmask 255.0.0.0
    ifconfig eth0 10.2.29.255 netmask 255.255.0.0
    route add default gw 10.2.1.1
    ifconfig eth0 down
    ifconfig eth0 hw ether 61:55:60:62:4e:bc
    ifconfig eth0 up
    ifconfig eth0 |grep Hwaddr

    I don't know if qt already has function like this or of it would be possible in cpp.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      For accessing .ini file you can use QSettings with format set to QSettings::IniFormat.
      You can retrieve the settings using value() member.

      For running external apps you can use QProcess.

      W 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        For accessing .ini file you can use QSettings with format set to QSettings::IniFormat.
        You can retrieve the settings using value() member.

        For running external apps you can use QProcess.

        W Offline
        W Offline
        wowy
        wrote on last edited by
        #3

        @Chris-Kawa
        Ok, i will take a look at QSettings and value().

        QProcess is only for external apps or shell command ?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          QProcess won't run shell commands, but you can run the shell itself and pass a script or commands to it as parameters.

          W 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            QProcess won't run shell commands, but you can run the shell itself and pass a script or commands to it as parameters.

            W Offline
            W Offline
            wowy
            wrote on last edited by
            #5

            @Chris-Kawa
            Ok.
            Well i don't think i will be able to use QProcess because the script will not be able to use the data from the ini read with Qsettings if i understand.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It's very well possible. In pseudo code:

              QSettings s("path/to/settings/file.ini", QSettings::IniFormat);
              QString someValue = s.value("section/optionName").toString();
              QString someOtherValue = s.value("section/anotherOptionName").toString();
              
              QString commandToRun = QString("some command with param %1 and %2").arg(someValue, someOtherValue);
              
              QProcess p;
              p.start("sh", QStringList() << "-c" << commandToRun);
              p.waitForFinished();
              

              Another way is to start "sh" without params and then write the commands to its input stream using QProcess::write().

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wowy
                wrote on last edited by wowy
                #7

                Ok, thank you i will take a look at this ! (learning every day)

                EDIT : I tried :

                #include <QCoreApplication>
                #include <QSettings>
                #include <QProcess>

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

                //QSettings s("/mnt/SD/CHIP.INI", QSettings::IniFormat);
                //QString someValue = s.value("IP/ADDRESS").toString();
                
                QString commandToRun = QString("printf hello");
                
                QProcess p;
                p.start("sh", QStringList() << "-c" << commandToRun);
                p.waitForFinished();
                
                return a.exec();
                

                }

                I the application don't "printf hello" and don't close after this.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It probably does print it just not where you think it does. The output of the external process is not redirected automatically to the callers shell window. If you want to see what the process wrote call readAllStandardOutput to read its standard output. You can then output it in your app if you want to.

                  The app doesn't exit because you started an event loop (a.exec();) and never exited it. If you want to exit the app at this point just don't call exec.

                  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