Nominate our 2022 Qt Champions!

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

  • 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 :

    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 netmask
    ifconfig eth0 netmask
    route add default gw
    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.

  • Moderators

    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.

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

    QProcess is only for external apps or shell command ?

  • Moderators

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

  • @Chris-Kawa
    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.

  • Moderators

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

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

  • 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);
    return a.exec();


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

  • Moderators

    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.

Log in to reply