How to pass cmd line arguments to a running instance of mainwindow.
-
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(); }@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 -
@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.@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. -
@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 -
@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.@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 openedBut 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) -
@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 openedBut 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)@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. -
@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.Hi,
Are you looking for QFileOpenEvent ?
-
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.
-
Hi,
Are you looking for QFileOpenEvent ?
@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.
-
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.
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.
-
@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.
@ademmler I think
QtSingleApplicationis 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 likeif(!listen()) return 0; MainWindow w; w.show(); return app.exec();In the "listen()" part I try to connect a
QLocalServerwith 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.
QtSingleApplicationseems to have similar design. -
@ademmler I think
QtSingleApplicationis 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 likeif(!listen()) return 0; MainWindow w; w.show(); return app.exec();In the "listen()" part I try to connect a
QLocalServerwith 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.
QtSingleApplicationseems to have similar design. -
Hi,
Are you looking for QFileOpenEvent ?
-
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/o7Z0riL2kgEAfter 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(); } -
A ademmler has marked this topic as solved on