从Qt应用程序启动外部程序

时间:2013-03-31 20:37:27

标签: c++ linux qt

我正在尝试运行一个程序让它在ubuntu 12.04中启动一个外部linux程序。当它被输入终端窗口时该命令有效,但当它被放入QT 4.8中点击的按钮对象时,我似乎无法使它工作。

这是在终端窗口中输入时有效的代码。 (它访问网络摄像头并使用叠加层)。我想要做的是每当按下一个按钮时,它会拍摄网络摄像头并使用日期和时间作为文件名保存图像。)

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

我正在关注该文件 HOWTO:从Qt应用程序启动外部程序
http://www.qtforum.org/article/3079/howto-start-an-external-program-from-a-qt-application.html

但是当我将代码添加到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";
}

它没有编译,我收到错误。本教程是否正确?

请参阅错误

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

我不认为发布所有代码都是正确的礼仪所以我只是发布了错误和我遇到问题的按钮代码。我应该发布所有代码吗?

如果这有帮助,这是main.cpp

#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();
}

3 个答案:

答案 0 :(得分:7)

一些事情:

  1. 使用指针创建流程:

    QProcess * process = new QProcess(命令,参数);

  2. 对命令使用QString,为参数使用QStringList

    QString命令=&#34; gst-launch-0.10&#34 ;; QStringList参数; 参数&lt;&lt; &#34;参数1&#34; &LT;&LT; &#34;参数2&#34; &LT;&LT; &#34;参数3&#34 ;;

  3. 启动正常分离的过程:

    工序 - &GT;开始(); 工序 - &GT; startDetached();

答案 1 :(得分:1)

你必须有一个命令和一个参数列表,所以它应该像

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

并且必须将commandAndParameters定义为您使用它的QStringList。从您的源代码中,您不清楚定义变量的位置以及使用它们的位置。

答案 2 :(得分:0)

您粘贴的代码和您粘贴的错误似乎不匹配。