C++ 在 Qt 应用程序中获取命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2918353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Obtaining command line arguments in a Qt application
提问by morpheous
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.
以下代码片段来自我使用 Qt 框架编写的一个小应用程序。这个想法是应用程序可以以批处理模式运行(即由脚本调用)或可以交互运行。
It is important therefore, that I am able to parse command line arguments in order to know which mode in which to run etc.
因此,重要的是我能够解析命令行参数,以便知道在哪种模式下运行等。
[Edit]
[编辑]
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).
我正在 Ubuntu Karmic 上使用 Qt Creator 1.3.1 进行调试。参数以正常方式传递(即通过 Qt Creator IDE 中的“项目”设置添加它们)。
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.
当我运行应用程序时,似乎参数没有传递给应用程序。下面的代码是我的 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;
}
[Update]
[更新]
I have identified the problem - for some reason, although argc is correct, the elements of argv are empty strings.
我已经确定了问题 - 出于某种原因,虽然 argc 是正确的,但 argv 的元素是空字符串。
I put this little code snippet to print out the argv items - and was horrified to see that they were all empty.
我把这个小代码片段打印出 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;
}
Does anyone know how I can retrieve the command line args in my application?
有谁知道如何在我的应用程序中检索命令行参数?
回答by HostileFork says dont trust SE
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:
如果您的 argc 和 argv 很好,我很惊讶这可能QApplication::arguments()
非常简单。注意源代码。过滤 Linux 的 #ifdefs,它只是:
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;
}
That's all you've got. There's a Unicode caveat which I would not think would apply to Karmic:
这就是你所拥有的。有一个 Unicode 警告,我认为它不适用于 Karmic:
"On Unix, this list is built from the argc and argv parameters passed to the constructor in the main() function. The string-data in argv is interpreted using QString::fromLocal8Bit(); hence it is not possible to pass, for example, Japanese command line arguments on a system that runs in a Latin1 locale. Most modern Unix systems do not have this limitation, as they are Unicode-based."
“在 Unix 上,这个列表是根据传递给 main() 函数中的构造函数的 argc 和 argv 参数构建的。argv 中的字符串数据是使用 QString::fromLocal8Bit() 解释的;因此它不可能传递,因为例如,在以 Latin1 语言环境运行的系统上的日语命令行参数。大多数现代 Unix 系统没有这个限制,因为它们是基于 Unicode 的。”
You might try a copy of that code against your argc and argv directly and see what happens.
您可以直接针对您的 argc 和 argv 尝试该代码的副本,看看会发生什么。
回答by Tobias Hunger
If you are writing a Console only application then you might want to consider using QCoreApplication instead of QApplicition. QCoreApplication is part of QtCore while QApplication is defined in QtGui, so you get an extra and unnecessary dependency.
如果您正在编写仅限控制台的应用程序,那么您可能需要考虑使用 QCoreApplication 而不是 QApplicition。QCoreApplication 是 QtCore 的一部分,而 QApplication 是在 QtGui 中定义的,因此您会获得额外且不必要的依赖项。
回答by lemmel
Only in order to keep response up-to-date, Qt now provides a dedicated class for parsing command line:
只是为了使响应保持最新,Qt 现在提供了一个专门的类来解析命令行:
http://doc.qt.io/qt-5/qcommandlineparser.html
http://doc.qt.io/qt-5/qcommandlineparser.html
P.S. : can only post this as response and not a comment; I'm sorry because the question was not really how to parse but how to access.
PS:只能将此作为回复而不是评论发布;我很抱歉,因为问题实际上不是如何解析,而是如何访问。