Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Starting a GUI program through Terminal with Arguments
QtWS25 Last Chance

[Solved] Starting a GUI program through Terminal with Arguments

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bobby B
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mkoskim
      wrote on last edited by
      #2

      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@

      http://mkoskim.wordpress.com
      http://mkoskim.drivehq.com
      http://mkoskim.deviantart.com

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RazrFalcon
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          try

          @
          man open
          @

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bobby B
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Bobby B
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

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

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved