Create an executable file with own fileextension



  • 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


  • Moderators

    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()).



  • 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?


  • Qt Champions 2016

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



  • 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.


  • Qt Champions 2016

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



  • 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.


  • Qt Champions 2016

    @mpergand
    Super. that is good info.



  • 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 ):



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.