tags:

views:

565

answers:

3

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 QTCreator 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 QTCreator 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?

A: 

What system is your program running on and how do you pass the arguments? Qt Creator is an IDE, not a system (in fact, it runs on three different ones).

P.S.: It's called Qt, not QT - the latter is QuickTime.

iconiK
+2  A: 

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.

Hostile Fork
@hostilefork: +1 for pointing me in the right direction. I was misled by argc being the correct value, and did not bother to dereference argv to check its contents. I put a little statement to print the arguments to stdout and I was horrified to see that there the elements of argv are empty strings - what sort of madness is this ?!
morpheous
I'll accept this as the correct answer since it started me on the right track - also, I cant delete the original question, since I voted this answer up. The original code was wrong in that I should be calling arguments on the instance of the application (rather than the class) - I don't know why I didn't spot this earlier (and no one did either). Also in the documentation arguments is a static method (and it compiles correctly [bizarrely] when called staticall), in reality I should be invoking it as an instance method (go figure!).
morpheous
@morpheous: there is no need to call arguments() on the instance of QCoreApplication as it's a static function. However, I find qApp->arguemtns() to be nicer than QCoreApplication::arguments().
iconiK
As iconiK says, it should not make a difference static or instance. (Note it uses "self" and not "this"...self is a Qt macro for dealing with single-instance classes.) As for your empty argv when argc is nonzero... uh, wtf? Is argv[argc] NULL?
Hostile Fork
A: 

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.

Tobias Hunger