Starting an external exe from Qt



  • Hi. I tried using the code from the example of the analog clock in the Qt manual after modifying it a little but it still has a logical error. The plan is that when I click a button, it is supposed to open an exe file. Would someone please show me a snippet of the correct implementation of the code.



  • external processes are started by QProcess.
    What did you do? can you show us your code so we can ahve a look at it?



  • Refer 'Running a Process' segment under the documentation of "QProcess":http://qt-project.org/doc/qt-4.8/qprocess.html

    Have you tried it already? Do you face any issue or error?



  • Thats where I got the snippet but after editing it to suit my app, it does not work. Here's the snippet:

    @
    QObject *parent;
    ...
    QString program = "C:/programs/bin/snort.exe";
    QStringList arguments;
    arguments << "-d";

     QProcess *myProcess = new QProcess(parent);
     myProcess->start(program, arguments);
    

    @

    [Edit: Added @ tags; mlong]



  • Moved to the General forum, as this is nothing for the C++ gurus :)



  • Would you tell us what is the error that you are getting or better send in the full code snippet.

    Also try the prefix and suffix your code by @ symbol, so that you get the code in good format
    @QObject *parent;

    QString program = “C:/programs/bin/snort.exe”;
    QStringList arguments; arguments << “-d”;

    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);@

    What is parent? It is not allocated with any value and that is being used as the parent for QProcess(). Remember you can pass '0' to QProcess as well or simply don't pass any value, which will default parent pointer to null.



  • A simple way in Windows would be:
    (in the slot for the button)
    @
    void MainWindow::on_pushButton_clicked()
    {
    system ("start C:\snort\bin\snort.exe");

    }
    @

    [edit] Added code tags, koahnig


  • Moderators

    [quote author="ommz" date="1339741582"]A simple way in Windows would be:
    (in the slot for the button)
    @
    void MainWindow::on_pushButton_clicked()
    {
    system ("start C:\snort\bin\snort.exe");

    }
    @
    [/quote]

    Certainly this is a way to do under windows, but it have another implementation for other OS.

    QProcess is wrapping the different methods and adds additional comfort. You can redirect" the input and output":http://qt-project.org/doc/qt-4.8/qprocess.html#communicating-via-channels through Qt.

    The equivalent to your simple program above would be using "the static execute method. ":http://qt-project.org/doc/qt-4.8/qprocess.html#execute-2

    @
    void MainWindow::on_pushButton_clicked()
    {
    QProcess::execute ("start C:\snort\bin\snort.exe");
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.