Starting an external program from a Qt application

10,647

Solution 1

A few things:

  1. Use a pointer for creating the process:

    QProcess *process = new QProcess(command, parameters);

  2. Use a QString for the command and a QStringList for the parameters:

    QString command = "gst-launch-0.10"; QStringList parameters; parameters << "param1" << "param2" << "param3";

  3. Start the process either normal or detached:

    process->start(); process->startDetached();

Solution 2

You must have a command and a parameter list, so it should something like

commandAndParameters << "gst-launch-0.10" << "v4l2src ! ..."

And commandAndParameters must be defined as QStringList where you use it. From your source code this is not clear where you define the variables and where you use them.

Share:
10,647
Rick T
Author by

Rick T

Updated on June 16, 2022

Comments

  • Rick T
    Rick T almost 2 years

    I'm trying to run a program to have it launch an external linux program in ubuntu 12.04. The command works when it's typed into a terminal window but I can't seem to get it to work when it's placed into a clicked button object in QT 4.8.

    Here's the code that works when typed in a terminal window. (It access the webcam and uses the overlay). What I'm trying to do is whenever a button is pressed it will take a picture of the webcam and save the image using the date and time as filenames)

    gst-launch-0.10 v4l2src ! video/x-raw-yuv, width=640, height=480  ! timeoverlay halignment=right valignment=bottom shaded-background=true ! clockoverlay halignment=left valignment=bottom text="M/D/Y:" shaded-background=true time-format="%m/%d/%Y %H:%M:%S" ! autovideosink
    

    I was following the document HOWTO: Start an external program from a Qt application
    http://www.qtforum.org/article/3079/howto-start-an-external-program-from-a-qt-application.html

    But when I add the code to the push button in QT

    void runprg2::on_pushButton_clicked()
    {
        commandAndParameters<<"gst-launch-0.10 v4l2src ! video/x-raw-yuv, width=640, height=480  ! timeoverlay halignment=right valignment=bottom shaded-background=true ! clockoverlay halignment=left valignment=bottom text="M/D/Y:" shaded-background=true time-format="%m/%d/%Y %H:%M:%S" ! autovideosink";
    }
    

    It doesn't compile and I get errors. Is this tutorial correct?

    See Errors

    ../test3/main.cpp: In function 'int main(int, char**)':
    ../test3/main.cpp:23:26: error: expected primary-expression before '<<' token
    ../test3/main.cpp:28:48: error: no matching function for call to 'QProcess::QProcess(QStringList&)'
    ../test3/main.cpp:28:48: note: candidates are:
    /usr/include/qt4/QtCore/qprocess.h:228:5: note: QProcess::QProcess(const QProcess&)
    /usr/include/qt4/QtCore/qprocess.h:228:5: note:   no known conversion for argument 1 from 'QStringList' to 'const QProcess&'
    /usr/include/qt4/QtCore/qprocess.h:133:14: note: QProcess::QProcess(QObject*)
    /usr/include/qt4/QtCore/qprocess.h:133:14: note:   no known conversion for argument 1 from 'QStringList' to 'QObject*'
    ../test3/main.cpp:31:25: error: no matching function for call to 'QProcess::start()'
    ../test3/main.cpp:31:25: note: candidates are:
    /usr/include/qt4/QtCore/qprocess.h:136:10: note: void QProcess::start(const QString&, const QStringList&, QIODevice::OpenMode)
    /usr/include/qt4/QtCore/qprocess.h:136:10: note:   candidate expects 3 arguments, 0 provided
    /usr/include/qt4/QtCore/qprocess.h:137:10: note: void QProcess::start(const QString&, QIODevice::OpenMode)
    /usr/include/qt4/QtCore/qprocess.h:137:10: note:   candidate expects 2 arguments, 0 provided
    make: *** [main.o] Error 1
    

    I didn't think posting all the code was proper etiquette so I just posted the error and the push button code that I had an issue with. Should I post all the code?

    Here's the main.cpp if this helps

    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include <qapplication.h>
    #include <qprocess.h>
    #include <qstringlist.h>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        // Set up the command line which starts the external program as QStringList.
            QStringList commandAndParameters;
    
            /* Fill in the following things:
             * - Name of program to execute.
             * - Every option needed by the program.
             * Attention: be aware of any strange options you pass to the program, e.g.
             * IP addresses. Quoting these options will usually help.
             */
           // commandAndParameters<<"konqueror"
                             <<"file:/home/thomas";
    
            /* Create a QProcess instance. It does not matter if it is created on the stack or
             * on the heap. - Ahem, I tested it on Linux only. :-)
             */
            QProcess myProcess(commandAndParameters);
    
            // Start the QProcess instance.
            myProcess.start();
    
            /* O.k., everything is fine now, leave the Qt application. The external program
             * will continue running.
             */
            return 0;
    
        return a.exec();
    }
    
  • Rick T
    Rick T about 11 years
    I didn't think posting all the code was proper etiquette so I just posted the error and the push button code. Should I post the whole thing?
  • Rick T
    Rick T about 11 years
    I added the main.cpp section to the question if that helps any
  • Dalibor Frivaldsky
    Dalibor Frivaldsky almost 10 years
    QProcess::startDetached is a static member function of the QProcess class, so "process->startDetached()" makes no sense