Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining command line arguments in a Qt application

Tags:

c++

qt

qt-creator

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?

like image 480
morpheous Avatar asked May 27 '10 03:05

morpheous


People also ask

How do you use command line arguments in Qt?

There is a Arguments line edit where you can put all you need to pass to your app when launching it. For Qt Creator from Qt 5.6 Go in the "Projects part on the left and then in the "Build & Run" tab. Here you have a "Command line arguments" edit where you can put all parameters you want to pass to your app.

What is command line arguments and explain it with example?

A command-line argument is nothing but the information that we pass after typing the name of the Java program during the program execution. These arguments get stored as Strings in a String array that is passed to the main() function. We can use these command-line arguments as input in our Java program.


2 Answers

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.

like image 167
lemmel Avatar answered Oct 07 '22 18:10

lemmel


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.

like image 45
Tobias Hunger Avatar answered Oct 07 '22 19:10

Tobias Hunger