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. Create an executable file with own fileextension
QtWS25 Last Chance

Create an executable file with own fileextension

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.1k 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
    Barcley
    wrote on last edited by
    #1

    Hi,

    iam developing an IDE for microcontrollers. When I create a new project in this application, a projectfile will be generated. Actually its a QSettings file with my suffix: .qtavr. (eg.: NewProject.qtavr)
    In this file I save some information when closing the application.

    Now I want to reopen the generated project by double clicking on this file. Therefor it should handover its stored information to the constructor of the mainWindow class.

    Is there a way to do something like this?

    greetings Barcley

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      If your OS has qtavr mime type associated with your application, it will call your app like this:

      yourapp.exe /path/to/doubleclicked/file.qtavr
      

      So all you need to do is to handle the input argument (using QApplication::arguments()).

      (Z(:^

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Barcley
        wrote on last edited by
        #3

        Iam using a MacOs.
        I've already told the OS to open files with this extension with the right app. But is it possible to do this automatically with for example an installer? Also it would be nice to give this file a special icon.

        When I try to catch the input argument from QApplication::arguments(), I just get the path of the application back, but not the path of the file.qtavr.

        QApplication::arguments() returns a QStringList. The path of the application I got with:

        arg = QApplication::arguments();
        ui->statusBar->showMessage(arg[0]);
        

        with:

        arg = QApplication::arguments();
        ui->statusBar->showMessage(arg[1]);
        

        the app is crashing.

        What am I doing wrong?

        mrjjM 1 Reply Last reply
        0
        • B Barcley

          Iam using a MacOs.
          I've already told the OS to open files with this extension with the right app. But is it possible to do this automatically with for example an installer? Also it would be nice to give this file a special icon.

          When I try to catch the input argument from QApplication::arguments(), I just get the path of the application back, but not the path of the file.qtavr.

          QApplication::arguments() returns a QStringList. The path of the application I got with:

          arg = QApplication::arguments();
          ui->statusBar->showMessage(arg[0]);
          

          with:

          arg = QApplication::arguments();
          ui->statusBar->showMessage(arg[1]);
          

          the app is crashing.

          What am I doing wrong?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Try check arg.size()
          for using [1] there should at least be 2 arguments.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            On MacOS you need to configure the Info.plist accordingly.
            Example for a pdf file

            <dict>
            			<key>CFBundleTypeExtensions</key>
            			<array>
            				<string>pdf</string>
            			</array>
            			<key>CFBundleTypeIconFile</key>
            			<string>pdf.icns</string>
            			<key>CFBundleTypeName</key>
            			<string>pdf document</string>
            			<key>CFBundleTypeRole</key>
            			<string>Editor</string>
            		</dict>
            

            A QFileOpenEvent will be generated, you need to implement the following in your Application subclass:

            bool Application::event(QEvent *event)
            { 
            	bool handled=false;
            
            	  switch (event->type())
            		{
            		case QEvent::FileOpen:
                                   {
            			QFileOpenEvent* openEvent=static_cast<QFileOpenEvent *>(event);
                   
            			proceedOpenFile(openEvent->file());
            			handled=true;
            			}
            			break;
            ....
            

            I handle the file in my proceedOpenFile method.

            mrjjM 1 Reply Last reply
            3
            • M mpergand

              On MacOS you need to configure the Info.plist accordingly.
              Example for a pdf file

              <dict>
              			<key>CFBundleTypeExtensions</key>
              			<array>
              				<string>pdf</string>
              			</array>
              			<key>CFBundleTypeIconFile</key>
              			<string>pdf.icns</string>
              			<key>CFBundleTypeName</key>
              			<string>pdf document</string>
              			<key>CFBundleTypeRole</key>
              			<string>Editor</string>
              		</dict>
              

              A QFileOpenEvent will be generated, you need to implement the following in your Application subclass:

              bool Application::event(QEvent *event)
              { 
              	bool handled=false;
              
              	  switch (event->type())
              		{
              		case QEvent::FileOpen:
                                     {
              			QFileOpenEvent* openEvent=static_cast<QFileOpenEvent *>(event);
                     
              			proceedOpenFile(openEvent->file());
              			handled=true;
              			}
              			break;
              ....
              

              I handle the file in my proceedOpenFile method.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mpergand
              so any parameters do not come as "arg" in MacOS but as QEvent::FileOpen ?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #7

                There are several ways to open a file, double click on the file, drag the file on the app icon in the Finder or in the Dock.
                In all this cases you 'll receive a FileOpen event.
                And no matter if your app is already launched or not.

                mrjjM 1 Reply Last reply
                3
                • M mpergand

                  There are several ways to open a file, double click on the file, drag the file on the app icon in the Finder or in the Dock.
                  In all this cases you 'll receive a FileOpen event.
                  And no matter if your app is already launched or not.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @mpergand
                  Super. that is good info.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Barcley
                    wrote on last edited by
                    #9

                    Hi,
                    thank you for your fast replies.

                    I tried this:

                    bool MainWindow::event(QEvent *event)
                    {
                        if (event->type() == QEvent::FileOpen) {
                            openEvent = static_cast<QFileOpenEvent *>(event);
                            qDebug() << "Open file" << openEvent->file();
                            
                            QFile tempFile(openEvent->file());
                            tempFile.setFileName("TEST");
                    
                            tempFile.open(QIODevice::ReadWrite);
                            QTextStream stream( &tempFile );
                            stream << "IWASHERE";
                            tempFile.close();
                        }
                        //return QApplication::event(event);
                    }
                    
                    

                    So if the Application is started by doubleklicking the foo.qtavr file, a new file named "TEST" should be generated. Unfortunately nothing has happened ):

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mpergand
                      wrote on last edited by mpergand
                      #10

                      Please read carefully what i wrote.
                      Moreover, it's clearly explain in the doc:

                      File open events will be sent to the QApplication::instance() when the operating system requests that a file or URL should be opened.
                      This is a high-level event that can be caused by different user actions depending on the user's desktop environment; for example, double clicking on an file icon in the Finder on OS X.

                      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