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. How to pass cmd line arguments to a running instance of mainwindow.
Qt 6.11 is out! See what's new in the release blog

How to pass cmd line arguments to a running instance of mainwindow.

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 1.6k Views 4 Watching
  • 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.
  • ademmlerA ademmler

    Hi,

    I am trying to find a way how to send command line arguments to an already running instance. Here is a minimal example - which works on initial launch.
    But how to send those arguments to MainWindow if this is already active?

    QtSingleApplication seems to be gone. What is the way to do this?
    The 2 examples in this forum did not work for me.

    #include <QApplication>
    #include <mainwindow.h>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        MainWindow w;
        w.show();
        if(argc > 1) {
            QStringList arguments = a.arguments();
            w.processArguments(arguments);
        }
        a.exec();
    }
    
    B Offline
    B Offline
    Bonnie
    wrote on last edited by
    #3

    @ademmler QtSingleApplication's code is still available in https://code.qt.io/cgit/qt-solutions/qt-solutions.git or https://github.com/qtproject/qt-solutions

    ademmlerA 1 Reply Last reply
    2
    • jsulmJ jsulm

      @ademmler said in How to pass cmd line arguments to a running instance of mainwindow.:

      But how to send those arguments to MainWindow if this is already active?

      What is the use case?
      And if you really need to do so then you can do that at any time because you can get them from QApplication instance (https://doc.qt.io/qt-6/qapplication.html#qApp) just like you do in the code snippet.

      ademmlerA Offline
      ademmlerA Offline
      ademmler
      wrote on last edited by
      #4

      @jsulm Dear JS. the use cases are:

      • Double click a file on the desktop and get it open
      • Drag a file on the application Icon and get it opened
      • Use contact menu to send a file to this application to get it opened

      All those on macOS as OnWindows.

      Thx for the hint to "qApp" - I gave it a try - and yes it delivers what I wanted.
      now I need to see how to achieve the above requirements.

      Pl45m4P 1 Reply Last reply
      0
      • B Bonnie

        @ademmler QtSingleApplication's code is still available in https://code.qt.io/cgit/qt-solutions/qt-solutions.git or https://github.com/qtproject/qt-solutions

        ademmlerA Offline
        ademmlerA Offline
        ademmler
        wrote on last edited by
        #5

        @Bonnie thx I check on this too!

        1 Reply Last reply
        0
        • ademmlerA ademmler

          @jsulm Dear JS. the use cases are:

          • Double click a file on the desktop and get it open
          • Drag a file on the application Icon and get it opened
          • Use contact menu to send a file to this application to get it opened

          All those on macOS as OnWindows.

          Thx for the hint to "qApp" - I gave it a try - and yes it delivers what I wanted.
          now I need to see how to achieve the above requirements.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #6

          @ademmler said in How to pass cmd line arguments to a running instance of mainwindow.:

          @jsulm Dear JS. the use cases are:

          Double click a file on the desktop and get it open
          Drag a file on the application Icon and get it opened
          Use contact menu to send a file to this application to get it opened

          But why you need a running instance for that?
          I know, there are some examples but from my experience, if you have your program running and you open a file connected to your program, in most cases a new window / instance of the program will start. (because it's a lot easier, I guess)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          ademmlerA 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @ademmler said in How to pass cmd line arguments to a running instance of mainwindow.:

            @jsulm Dear JS. the use cases are:

            Double click a file on the desktop and get it open
            Drag a file on the application Icon and get it opened
            Use contact menu to send a file to this application to get it opened

            But why you need a running instance for that?
            I know, there are some examples but from my experience, if you have your program running and you open a file connected to your program, in most cases a new window / instance of the program will start. (because it's a lot easier, I guess)

            ademmlerA Offline
            ademmlerA Offline
            ademmler
            wrote on last edited by ademmler
            #7

            @Pl45m4 hi, you made a good question.

            Let me try to describe in my own words:
            main does initiate a "mainwindow" as an instance.

            When I double click a file or do some of the other actions above I want to open this file in exact this already open window.
            Hence somehow I need to tell the running instance about it. Right?

            As what already got this approach might differ on MacOS and Windows.

            I did what @jsulm recommended and added this to my MainWindow routine.

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                QStringList arguments = qApp->arguments();
                if(arguments.size() > 1) {
                    qDebug() << arguments.at(1);
                    processArguments(arguments);
                }
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::processArguments(QStringList arguments)
            {
                ui->plainTextEdit->clear();
                ui->plainTextEdit->appendPlainText(arguments.join("\n"));
            }
            

            This work only on initial launch of the application,
            but not if the application is already running.

            SGaistS 1 Reply Last reply
            0
            • ademmlerA ademmler

              @Pl45m4 hi, you made a good question.

              Let me try to describe in my own words:
              main does initiate a "mainwindow" as an instance.

              When I double click a file or do some of the other actions above I want to open this file in exact this already open window.
              Hence somehow I need to tell the running instance about it. Right?

              As what already got this approach might differ on MacOS and Windows.

              I did what @jsulm recommended and added this to my MainWindow routine.

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  QStringList arguments = qApp->arguments();
                  if(arguments.size() > 1) {
                      qDebug() << arguments.at(1);
                      processArguments(arguments);
                  }
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              void MainWindow::processArguments(QStringList arguments)
              {
                  ui->plainTextEdit->clear();
                  ui->plainTextEdit->appendPlainText(arguments.join("\n"));
              }
              

              This work only on initial launch of the application,
              but not if the application is already running.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Hi,

              Are you looking for QFileOpenEvent ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              ademmlerA 2 Replies Last reply
              2
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #9

                This reminds me of the old days when applications still used MDI (multiple documents interface). Qt seems to still support this. It is probably not what you want, but this would have the same kind of problem to open files inside an existing window. So, searching for MDI explicitly might give you some pointers on how to handle this.

                ademmlerA 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Are you looking for QFileOpenEvent ?

                  ademmlerA Offline
                  ademmlerA Offline
                  ademmler
                  wrote on last edited by ademmler
                  #10

                  @SGaist thx for your response

                  yes I did - still struggling to get this work
                  Let me send a minimal example later.

                  But this is mac only - right?
                  Does it need separate implementation for windows than?

                  An I am wondering if i really need to implement

                  • QtSingleApplication
                  • FileOpenEvent
                  • qApp-Arguments

                  To get a simple "Double Click" or "Drag to Icon" experience. Looks like this.

                  B 1 Reply Last reply
                  0
                  • S SimonSchroeder

                    This reminds me of the old days when applications still used MDI (multiple documents interface). Qt seems to still support this. It is probably not what you want, but this would have the same kind of problem to open files inside an existing window. So, searching for MDI explicitly might give you some pointers on how to handle this.

                    ademmlerA Offline
                    ademmlerA Offline
                    ademmler
                    wrote on last edited by
                    #11

                    @SimonSchroeder

                    in my solution I can create multiple instances of the main window. The reason is by software concept. All processing parameters are bound to the window . It is like "Workspaces" with a fixed set of settings.

                    1 Reply Last reply
                    0
                    • ademmlerA ademmler

                      @SGaist thx for your response

                      yes I did - still struggling to get this work
                      Let me send a minimal example later.

                      But this is mac only - right?
                      Does it need separate implementation for windows than?

                      An I am wondering if i really need to implement

                      • QtSingleApplication
                      • FileOpenEvent
                      • qApp-Arguments

                      To get a simple "Double Click" or "Drag to Icon" experience. Looks like this.

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #12

                      @ademmler I think QtSingleApplication is more suitable for that.
                      I had written code to make "double click" activate the existing instance's window, which was requested by the client.
                      Something like

                          if(!listen())
                              return 0;
                          MainWindow w;
                          w.show();
                          return app.exec();
                      

                      In the "listen()" part I try to connect a QLocalServer with certain name. If connected, I'll send some information to it (in you case you can send the arguments) and return false. If such server is not found, I'll create one, make it listening and return true.
                      QtSingleApplication seems to have similar design.

                      ademmlerA 1 Reply Last reply
                      2
                      • B Bonnie

                        @ademmler I think QtSingleApplication is more suitable for that.
                        I had written code to make "double click" activate the existing instance's window, which was requested by the client.
                        Something like

                            if(!listen())
                                return 0;
                            MainWindow w;
                            w.show();
                            return app.exec();
                        

                        In the "listen()" part I try to connect a QLocalServer with certain name. If connected, I'll send some information to it (in you case you can send the arguments) and return false. If such server is not found, I'll create one, make it listening and return true.
                        QtSingleApplication seems to have similar design.

                        ademmlerA Offline
                        ademmlerA Offline
                        ademmler
                        wrote on last edited by
                        #13
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Hi,

                          Are you looking for QFileOpenEvent ?

                          ademmlerA Offline
                          ademmlerA Offline
                          ademmler
                          wrote on last edited by ademmler
                          #14
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • ademmlerA Offline
                            ademmlerA Offline
                            ademmler
                            wrote on last edited by ademmler
                            #15

                            Hi all, thx to everybody for helping.

                            I mark @SGaist answer as the right one, because he made me investigate again and deeper into QFileOpenEvent.

                            From this nice tutorial - at the very end - I have got the details, which are
                            needed to get QFileOpenEvent work on MacOS: https://youtu.be/o7Z0riL2kgE

                            After overwriting QApplication you can do this in main.cpp

                            #include <mainwindow.h>
                            #include "myapplication.h"
                            
                            int main(int argc, char *argv[])
                            {
                                MyApplication a(argc, argv);
                                auto arguments = a.arguments();
                            
                                MainWindow w;
                                w.show();
                            
                            #ifdef Q_OS_MACOS
                                QObject::connect(&a, &MyApplication::openFile,
                                                 &w, &MainWindow::messageSend);
                            #else
                                w.messageSend(arguments.at(1));
                            #endif
                            
                                return a.exec();
                            }
                            
                            1 Reply Last reply
                            0
                            • ademmlerA ademmler has marked this topic as solved on

                            • Login

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