简体   繁体   中英

Unable to open file with qt app on mac

I am trying to associate custom files with an osx app. I have a plist that associates files with the app, but double clicking a file opens the app with no data inside.

Calling

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

from the terminal opens the file correctly inside the app.

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 should somehow implement QFileOpenEvent ... how ? This example looks good... but I don't understand how to combine the constructor and the event...

How do I make this work ?

(OS X 10.6-10.9, app created using Qt 4.8)

The following is the adapted code, that will respond to the OpenFileEvent either on start or during the normal functioning - and also allow opening o file 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);
 }

i'm using a QApplication derived class that emits a signal when 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 created OpenWindowApplication with the MainWindow object so once the file is ready, the 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 connects fileReady signal with a lambda func that opens the file and updates widget

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

here's the result: 4inline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM