Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在Qt应用程序中获取命令行参数_C++_Qt_Qt Creator - Fatal编程技术网

C++ 在Qt应用程序中获取命令行参数

C++ 在Qt应用程序中获取命令行参数,c++,qt,qt-creator,C++,Qt,Qt Creator,下面的代码片段来自我使用Qt框架编写的一个小应用程序。其理念是,应用程序可以在批处理模式下运行(即通过脚本调用),也可以交互运行 因此,重要的是,我能够解析命令行参数,以便知道在哪种模式下运行等等 [编辑] 我正在Ubuntu Karmic上使用Qt Creator 1.3.1进行调试。参数以正常方式传递(即通过Qt Creator IDE中的“项目”设置添加参数) 当我运行应用程序时,似乎没有将参数传递给应用程序。下面的代码是我的main()函数的一个片段 int main(int argc,

下面的代码片段来自我使用Qt框架编写的一个小应用程序。其理念是,应用程序可以在批处理模式下运行(即通过脚本调用),也可以交互运行

因此,重要的是,我能够解析命令行参数,以便知道在哪种模式下运行等等

[编辑]

我正在Ubuntu Karmic上使用Qt Creator 1.3.1进行调试。参数以正常方式传递(即通过Qt Creator IDE中的“项目”设置添加参数)

当我运行应用程序时,似乎没有将参数传递给应用程序。下面的代码是我的main()函数的一个片段

int main(int argc, char *argv[])
{
    //Q_INIT_RESOURCE(application);

    try {
        QApplication the_app(argc, argv);

        //trying to get the arguments into a list    
        QStringList cmdline_args = QCoreApplication::arguments();

        // Code continues ...
    }
    catch (const MyCustomException &e) { return 1; }

    return 0;
}
[更新]

我已经发现了这个问题——出于某种原因,尽管argc是正确的,但是argv的元素是空字符串

我把这个小代码片段打印出来的argv项目-并惊骇地看到,他们都是空的

for (int i=0; i< argc; i++){
    std::string s(argv[i]); //required so I can see the damn variable in the debugger
    std::cout << s << std::endl;
}
for(int i=0;istd::cout如果您的argc和argv都很好,我很惊讶这是可能的,因为
QApplication::arguments()
非常简单。注意。过滤Linux的#ifdef,它只是:

QStringList QCoreApplication::arguments()
{
    QStringList list;
    if (!self) {
        qWarning("QCoreApplication::arguments: Please instantiate the QApplication object first");
        return list;
    }
    const int ac = self->d_func()->argc;
    char ** const av = self->d_func()->argv;
    for (int a = 0; a < ac; ++a) {
        list << QString::fromLocal8Bit(av[a]);
    }
    return list;
}
QStringList QCoreApplication::arguments()
{
QStringList表;
如果(!self){
qWarning(“QCoreApplication::arguments:请先实例化QApplication对象”);
退货清单;
}
const int ac=self->d_func()->argc;
char**const av=self->d_func()->argv;
对于(int a=0;a列表< p>如果您只编写控制台应用程序,那么您可能需要考虑使用QCORePoT而不是QualPixy。QCOReApple是QtCar的一部分,而QApple是在QtGui中定义的,因此您得到了额外的和不必要的依赖性。

< P>只是为了保持响应的最新,QT现在提供了PARSI专用的类。ng命令行:


备注:只能将此作为回复而不是评论发布;很抱歉,问题不是如何解析,而是如何访问。

这里是一个简单的例子,可以将参数放在QStringList中。假设您使用参数-q-t启动应用程序

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

QString x;

for (int i=1; i<argc; i++)
{
    x.append(argv[i]);
}
qDebug() << x;
QStringList args = x.split("-");
args.removeFirst();
qDebug() << "args="<< args;
return a.exec();
}
现在你有了作为QStringList的参数

这是我编写的完整代码,并在一个小应用程序中使用

#include "mainwindow.h"

#include <QApplication>
#include <QDebug>
static  QStringList arguments;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
//analyze the arguments
//-b: use buidin player and if it does not exist use mpg123 shell to play files
//
//-t: test the player upon startup and exit
//-s: use the following speaker ID for the test
//-f: use the following file name and path
//syntax: example:
//                  -b : to use build in player
//                  -t -s xx:xx:xx:xx:xx -f azanfile.mp3: to test upon startup playing a file


    bool useBuildInPlayer;

    QString x;
    for (int i=1; i<argc; i++)
    {
        x.append(argv[i]);
    }
    arguments << x.split("-");    arguments.removeFirst();
    qDebug() << arguments;

    if (arguments.indexOf("b")>=0) 
        useBuildInPlayer=true;
    else                            
        useBuildInPlayer=false;

    bool TestSpeaker = false;
    bool spkr=false; QString speaker;
    bool playfile=false;  QStringList testfiles;
    QString filestring;
    foreach (QString x, arguments)
    {
        if (x.left(1)=="s")
        {
            speaker = x.mid(1,-1); //remove the s from the beginning
            spkr=true;
        }
        if (x.left(1)=="f")
        {
            filestring=x.mid(1,-1);
            playfile=true;
            testfiles<<filestring;
        }
        if (x=="t")
            TestSpeaker = true;
    }

    if (TestSpeaker)
    {
        if (spkr)
        {
            qDebug() << "testing speaker "<< speaker;
        }
        else
        {
            qDebug() << "test argument needs speaker -s xx:xx:xx:xx:xx";
        }
        if (playfile)
        {
            qDebug() << "testing file "<< filestring;
        }
        else
        {
            qDebug() << "test file is missing";
        }
    }
    if (TestSpeaker && spkr && playfile)
    {

        if (useBuildInPlayer) //use build in player
        {
            qDebug() << "testing using buildin player";

        }
        else // use mpg123 shell
        {
            qDebug() << "testing using mpg123 shell";

        }
    }


    return a.exec();
}
#包括“mainwindow.h”
#包括
#包括
静态QStringList参数;
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
w、 show();
//分析论点
//-b:使用内置播放器,如果不存在,则使用mpg123 shell播放文件
//
//-t:在启动和退出时测试播放器
//-s:使用以下扬声器ID进行测试
//-f:使用以下文件名和路径
//语法:示例:
//-b:使用内置播放器
//-t-s xx:xx:xx:xx:xx-f azanfile.mp3:在启动时播放文件时进行测试
bool-useBuildInPlayer;
QString x;

对于(int i=1;i您在try/catch上有一个额外的大括号。您没有为我们提供实用工具::选项\u值的代码…您能否将此示例简化为说如果cmdline_args.isEmpty()打印“哦,不!”?如果你这样做了,你还会遇到问题吗?顺便问一下,什么是utility::options_值?我在Qt文档中也没有看到类似的内容。而且,即使你不传递参数,第一个参数始终是程序名。所以我想问题可能出在utility::option_值中。你也试过检查argv[0],argv[1]等等…也试试这个..+1,因为它给我指出了正确的方向。我被argc是正确的值误导了,没有费心去引用argv来检查它的内容。我放了一个小语句来打印参数到stdout,我惊骇地看到argv的元素是空字符串-这是什么疯狂?!我我承认这是正确的答案,因为它让我走上了正确的轨道——而且,我不能删除原来的问题,因为我投票支持了这个答案。原来的代码是错误的,因为我应该在应用程序实例(而不是类)上调用参数——我不知道为什么我之前没有发现这个问题(也没有人发现)。在文档中,参数也是一个静态方法(当调用staticall时,它编译正确[奇怪]),实际上我应该将其作为一个实例方法调用(见图!)。@morphase:QCoreApplication的实例上不需要调用参数(),因为它是一个静态函数。但是,我发现qApp->arguemtns()比QCoreApplication::arguments()更好。正如iconiK所说,它不应该对静态或实例产生影响。(注意,它使用“self”而不是“this”…self是处理单实例类的Qt宏。)至于argc为非零时的空argv…呃,wtf?argv[argc]为NULL吗?
#include "mainwindow.h"

#include <QApplication>
#include <QDebug>
static  QStringList arguments;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
//analyze the arguments
//-b: use buidin player and if it does not exist use mpg123 shell to play files
//
//-t: test the player upon startup and exit
//-s: use the following speaker ID for the test
//-f: use the following file name and path
//syntax: example:
//                  -b : to use build in player
//                  -t -s xx:xx:xx:xx:xx -f azanfile.mp3: to test upon startup playing a file


    bool useBuildInPlayer;

    QString x;
    for (int i=1; i<argc; i++)
    {
        x.append(argv[i]);
    }
    arguments << x.split("-");    arguments.removeFirst();
    qDebug() << arguments;

    if (arguments.indexOf("b")>=0) 
        useBuildInPlayer=true;
    else                            
        useBuildInPlayer=false;

    bool TestSpeaker = false;
    bool spkr=false; QString speaker;
    bool playfile=false;  QStringList testfiles;
    QString filestring;
    foreach (QString x, arguments)
    {
        if (x.left(1)=="s")
        {
            speaker = x.mid(1,-1); //remove the s from the beginning
            spkr=true;
        }
        if (x.left(1)=="f")
        {
            filestring=x.mid(1,-1);
            playfile=true;
            testfiles<<filestring;
        }
        if (x=="t")
            TestSpeaker = true;
    }

    if (TestSpeaker)
    {
        if (spkr)
        {
            qDebug() << "testing speaker "<< speaker;
        }
        else
        {
            qDebug() << "test argument needs speaker -s xx:xx:xx:xx:xx";
        }
        if (playfile)
        {
            qDebug() << "testing file "<< filestring;
        }
        else
        {
            qDebug() << "test file is missing";
        }
    }
    if (TestSpeaker && spkr && playfile)
    {

        if (useBuildInPlayer) //use build in player
        {
            qDebug() << "testing using buildin player";

        }
        else // use mpg123 shell
        {
            qDebug() << "testing using mpg123 shell";

        }
    }


    return a.exec();
}