Solved Open file in already open App
-
Hi,
I am working on a tabbed code editor. I managed to associate a file type with my app but when I double click a second file the app started again. The idea was that the second file would be opened in the already open app as a second tab.
This is my main.cpp. How can I modify it so that no duplicates of this app are started?
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { Q_INIT_RESOURCE(qrc); QApplication app(argc, argv); app.setStyleSheet("QSplitter::handle { background-color: #999999 }"); QCoreApplication::setOrganizationName("innoBiz"); QCoreApplication::setApplicationName("phpPad"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("file", "The file to open."); parser.process(app); MainWindow mainWin; if (!parser.positionalArguments().isEmpty()){ QString path = parser.positionalArguments().first(); path.replace(QString("\\"), QString("/")); mainWin.addEditor(path); } mainWin.show(); return app.exec(); }
I copied this code from the Application Example. Actually I do not really understand how this parser thing works...
Thanks for any help.
-
@Sikarjan Maybe this is of interest: https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication
-
Thanks for the link. I guess that is what I need.
-
Hi,
While the suggestion is correct, you should rather use http://code.qt.io/cgit/qt-solutions/qt-solutions.git/ which is the official repository. Github is just a mirror.
-
If been trying to get the qtsingleapplication class to work with no success. Is there another option out there, which is easier to implement?
-
I got the qtsingleapplicaiton "plugin" to work. My main.cpp looks like this now:
#include "mainwindow.h" #include <QApplication> #include <qtsingleapplication.h> int main(int argc, char *argv[]) { Q_INIT_RESOURCE(qrc); QtSingleApplication app(argc, argv); if (app.isRunning()){ QString message; for (int a = 1; a < argc; ++a) { message += argv[a]; if (a < argc-1) message += " "; } return !app.sendMessage(message); } app.setStyleSheet("QSplitter::handle { background-color: #999999 }"); QCoreApplication::setOrganizationName("innoBiz"); QCoreApplication::setApplicationName("phpPad"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("file", "The file to open."); parser.process(app); MainWindow mainWin; app.setActivationWindow(&mainWin); if (!parser.positionalArguments().isEmpty()){ QStringList files = parser.positionalArguments(); foreach(QString path, files){ path.replace(QString("\\"), QString("/")); mainWin.addEditor(path); } } mainWin.show(); QObject::connect(&app, SIGNAL(messageReceived(const QString&)), &mainWin,SLOT(handleAppOpenMessage(const QString&))); return app.exec(); }
In my MainWindow I added the slot handleAppOpenMessage(QString message) which does the same as the for each loop above
message.replace(QString("\\"), QString("/")); addEditor(path);
This is probably not a very nice bit of code but currently is is working on Windows. If someone likes to make a suggestion on how to do this better, I am open to feedback.
I believe the for loop to build the message is not required. If I try to open e.g. three files on windows three messages are send and not one with three files.
-
You can use QDir::fromNativeSeparators to convert your paths.