在Qt应用程序中获取命令行参数 [英] Obtaining command line arguments in a Qt application

查看:1316
本文介绍了在Qt应用程序中获取命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following snippet is from a little app I wrote using the Qt framework. The idea is that the app can be run in batch mode (i.e. called by a script) or can be run interactively.

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

It is important therefore, that I am able to parse command line arguments in order to know which mode in which to run etc.

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

I am debugging using Qt Creator 1.3.1 on Ubuntu Karmic. The arguments are passed in the normal way (i.e. by adding them via the 'Project' settings in the Qt Creator IDE).

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

When I run the app, it appears that the arguments are not being passed to the application. The code below, is a snippet of my main() function.

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的元素是空字符串。

I have identified the problem - for some reason, although argc is correct, the elements of argv are empty strings.

小小的代码段可以打印出argv项-并震惊地发现它们都是空的。

I put this little code snippet to print out the argv items - and was horrified to see that they were all empty.

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;
}

有人知道我如何在应用程序中检索命令行参数吗? / p>

Does anyone know how I can retrieve the command line args in my application?

推荐答案

如果您的argc和argv都很好,那么我很惊讶,因为 QApplication :: arguments()非常简单。注意源代码。过滤用于Linux的#ifdef,只是:

If your argc and argv are good, I'm surprised this would be possible as QApplication::arguments() is extremely simple. Note the source code. Filtering the #ifdefs for Linux, it's just:

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;
}

这就是您所拥有的。有一个Unicode警告,我认为这不适用于Karmic:

That's all you've got. There's a Unicode caveat which I would not think would apply to Karmic:

在Unix上,此列表是根据传递给构造函数的argc和argv参数构建的argv中的字符串数据使用QString :: fromLocal8Bit()进行解释;因此,例如,在以Latin1语言环境运行的系统上,无法传递日文命令行参数。 Unix系统没有此限制,因为它们基于Unicode。

您可以直接在argc和argv上尝试复制该代码,看看会发生什么。

You might try a copy of that code against your argc and argv directly and see what happens.

这篇关于在Qt应用程序中获取命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆