Unable to open file with qt application on mac

I am trying to link custom files to an osx application. I have a plist that links files to an application, but double clicking on the file opens the application without any data inside.

Call

someapp.app/Contents/MacOs/someapp somefile.abc

from terminal opens the file correctly inside the application.

MyApp::MyApp(int& argc, char**argv): QApplication(argc, argv)
{
  ...
  m_MainWindow = new MainWindows();
  m_MainWindow->show();
  if(argc > 1 && argv[1])
      m_MainWindow->openFile(QString(argv[1]);
  else 
      m_MainWindow->showStartupDialog();  // to create a new document
}

      

Searching around I found that I have to implement QFileOpenEvent somehow ... how? This example looks pretty good ... but I don't understand how to combine constructor and event ...

How do I make this work?

(OS X 10.6-10.9, application built using Qt 4.8)

+3


source to share


2 answers


Below is the adapted code that will respond OpenFileEvent

either on startup or during normal operation - as well as allow opening file o from command line or creating a new file



MyApp::MyApp(int& argc, char**argv): QApplication(argc, argv)
{
  ...
  m_MainWindow = new MainWindows();
  m_MainWindow->show();
  if(argc > 1 && argv[1])
      m_MainWindow->openFile(QString(argv[1]);
  else if (m_macFileOpenOnStart != "")
      m_MainWindow->openFile(m_macFileOpenOnStart);  // open file on start if it exists
  else 
      m_MainWindow->showStartupDialog();  // to create a new document
}

 // responds to FileOpenEvent specific for mac
 bool MyApp::event(QEvent *event)
 {
    switch(event->type())
    {
    case QEvent::FileOpen:
    {
        QFileOpenEvent * fileOpenEvent = static_cast<QFileOpenEvent *>(event);
        if(fileOpenEvent)
        {
            m_macFileOpenOnStart = fileOpenEvent->file();
            if(!m_macFileOpenOnStart.isEmpty())
            {
                if (m_MainWindow)
                {
                    m_MainWindow->openFile(m_macFileOpenOnStart);  // open file in existing window
                }
                return true;
            }
        }
    }
    default:
        return QApplication::event(event);
    }
    return QApplication::event(event);
 }

      

+3


source


I am using a class derived from QApplication that emits a signal when the file is ready:

#ifndef OPENWITHAPPLICATION_H
#define OPENWITHAPPLICATION_H

#include <QApplication>
#include <QFileOpenEvent>
#include <QMessageBox>


class OpenWithApplication : public QApplication
{
    Q_OBJECT

public:
    QString fileName;

    OpenWithApplication(int &argc, char **argv)
        : QApplication(argc, argv)
    {
    }
signals:

    void fileReady(QString fn);

protected:

    bool event(QEvent *event)
    {
        if (event->type() == QEvent::FileOpen) {
            QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
            fileName = openEvent->file();

            emit fileReady(fileName); //  the file is ready
        }

        return QApplication::event(event);
    }
};

#endif // OPENWITHAPPLICATION_H

      

main.cpp connects the created OpenWindowApplication to the MainWindow object, so as soon as the file is ready, a signal is emitted and received by it for processing

#include "mainwindow.h"
#include <openwithapplication.h>

int main(int argc, char *argv[])
{
    OpenWithApplication a(argc, argv);
    MainWindow w;

    w.connectOpenWithApp(&a);
    w.show();

    return a.exec();
}

      



and MainWindow hooks up the fileReady signal with a lambda function that opens the file and updates the widget

void MainWindow::connectOpenWithApp(OpenWithApplication*app) {
    connect(app, &OpenWithApplication::fileReady, [this](QString fileName){
        bw->open(fileName);
        bw->update();
    });
}

      

here is the result: 4inline

0


source







All Articles