[Solved] Starting a GUI program through Terminal with Arguments



  • Hi,

    I am trying to develop a program that can be opened through the terminal on the Mac (I believe the general term is the console).
    For example, I want to enter the command with some arguments such as the one shown below
    @
    open myProgram.app -o
    @
    Where myProgram.app should be executed and start dealing with the -o argument before the main window opens.

    I have implemented something similar to the code below in the main.cpp file
    @
    #include <QtGui/QApplication>
    #include <QTextStream>

    #include "mainwindow.h"

    int main(int argc, char *argv[]){
    QFile *stdOutFile = new QFile();
    stdOutFile->open(stdout, QIODevice::WriteOnly);
    QTextStream stdOutStream(stdOutFile);

    QApplication a(argc, argv);
    MainWindow w;

    for (int i=0; i<argc; i++){
    if (QString(argv[i] == "-o")
    stdOutStream << "Let's dance\n";
    else
    stdOutStream << "Take a nap\n";
    }

    stdOutStream.setDevice(0);
    stdOutFile->close();
    delete stdOutFile;

    w.show();
    return a.exec();
    }
    @

    I have also included
    @
    QT += console
    @
    in the .pro file.

    I have tried this but the problem is that the terminal does not recognize the -o command and assumed it was an option for the open command. A small snippet of the output produced is shown below.
    @
    open: invalid option --o
    Usage: open [-e] [-t] [-f] [-W] //and it goes on for a bit
    @

    I am fairly new to using Mac's terminal and only have experience working with Windows' Command Prompt so I might be doing something terribly wrong here.
    If any of you know the reason why it doesn't work or know a better way of doing this I would be thankful if you would share.



  • I don't know anything about Mac terminal and shell it uses, but if it resembles Linux, then you could try to put the command with options in some sort of quotes (to pass the entire command with options as single argument to open command), something like:

    @open "myProgram -o -x -y -z" fileToOpen@



  • Try to use "arguments()":http://doc.qt.nokia.com/latest/qcoreapplication.html#arguments insted of argc.



  • try

    @
    man open
    @

    from the command line. It shows you what to do (hint: look for --args)



  • Thanks Volker, I had totally overlooked that and I feel silly now. So the specific command was actually
    @ open myProgram.app --args -o @

    Something I also notice was that no text is outputted onto the Terminal until the line
    @ stdOutStream.setDevice(0);@ was executed. I checked the documentation and I think it had something to do with QTextStream calling flush().

    However, since I'm only dealing with handling arguments from the Terminal I can work with that by making sure to unset the device before I do any return statement. For example, I could implement a -help option that would simply return to the terminal instead of opening the GUI, but I would have to make sure to unset the device I guess.

    If anyone else has a better idea I would be glad to hear from you.



  • It depends on what you try to do. If the stdout output is only for debugging purposes, I recommend using [[Doc:QDebug]], it has the advantage, that it is aware of many of the Qt builtin types and formats them nicely.

    If you need the text stream the way you posted it, it might be helpful to switch to unbuffered mode:

    @
    file->open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);
    @

    On the Mac things become more complicated: in case you start the program with open utility, it is opened in a second terminal which gets all the output. That terminal is closed as soon as your program terminates. So, even if you're able to print the help message, it's output vanishes from the screen immediately. The only way is to call the program directly, not using open.



  • Thanks Volker, the QIODevice::Unbuffered setting worked.

    I tried using the open command on the terminal and it seems that it does open another terminal when the program is run.

    It seems that I am truly ignorant on how the Terminal works for all my Googling that I have done just turns up saying to use the open command.

    But this is starting to sound more like a problem with my ignorance with the Mac. I believe that everything that I needed for this particular problem has been provided. Thank you for your help everyone, I really appreciate it.



  • You're welcome. Mac behaves sometimes like an alien - it feels a bit strange at first, but one gets used to it :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.