Obtaining command line arguments in a Qt applicati

2020-02-09 04:15发布

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.

[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).

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

[Update]

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

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

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

3条回答
贼婆χ
2楼-- · 2020-02-09 04:34

Only in order to keep response up-to-date, Qt now provides a dedicated class for parsing command line:

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.

查看更多
姐就是有狂的资本
3楼-- · 2020-02-09 04:43

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.

查看更多
家丑人穷心不美
4楼-- · 2020-02-09 04:48

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

That's all you've got. There's a Unicode caveat which I would not think would apply to 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."

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

查看更多
登录 后发表回答