2

我正在尝试运行一个程序,让它在 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: Start an external program from a Qt application
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();
}
4

3 回答 3

7

一些东西:

  1. 使用指针创建进程:

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

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

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

  3. 启动进程正常分离

    进程->开始();进程->startDetached();

于 2013-03-31T21:25:02.153 回答
1

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

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

并且commandAndParameters必须在您使用它的地方定义为 QStringList。从您的源代码中,不清楚您在哪里定义变量以及在哪里使用它们。

于 2013-03-31T20:55:14.690 回答
0

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

于 2013-03-31T20:41:34.633 回答