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. Open file in already open App
Forum Updated to NodeBB v4.3 + New Features

Open file in already open App

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.4k Views 2 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.
  • SikarjanS Offline
    SikarjanS Offline
    Sikarjan
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • SikarjanS Sikarjan

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Sikarjan Maybe this is of interest: https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SikarjanS Offline
        SikarjanS Offline
        Sikarjan
        wrote on last edited by
        #3

        Thanks for the link. I guess that is what I need.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

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

          1 Reply Last reply
          0
          • SikarjanS Offline
            SikarjanS Offline
            Sikarjan
            wrote on last edited by
            #5

            If been trying to get the qtsingleapplication class to work with no success. Is there another option out there, which is easier to implement?

            1 Reply Last reply
            0
            • SikarjanS Offline
              SikarjanS Offline
              Sikarjan
              wrote on last edited by Sikarjan
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You can use QDir::fromNativeSeparators to convert your paths.

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

                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