在Mac上使用Qt应用程序无法打开文件

3
我想将自定义文件与一个OSX应用程序关联起来。我有一个plist文件将文件与该应用程序关联,但是双击文件时打开的应用程序内没有数据。
从终端调用
someapp.app/Contents/MacOs/someapp somefile.abc
可以正确地在应用程序中打开文件。
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
}

在搜索过程中,我发现应该以某种方式实现QFileOpenEvent...但是怎么做呢?这个示例看起来不错...但我不明白如何将构造函数和事件组合起来...

我该怎么做才能让它工作?

(OS X 10.6-10.9,使用Qt 4.8创建的应用程序)

2个回答

3
以下是改编后的代码,它将在启动时或正常运行期间响应 OpenFileEvent - 并且还允许从命令行打开文件或创建新文件。
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);
 }

你能解释一下为什么这个例子有一个default开关/情况返回以及一个标准函数返回吗?这不是多余的吗? - tresf
@QZSupport - 可能它可以被写得更好一些,只需要在默认情况下添加一个"break"就可以了,这确实是同样的事情。函数返回值不是多余的,因为在第一个情况下没有处理的路径。 - Thalia
你能解释一下“函数返回值不是多余的”这个说法吗?难道第一个情况没有处理的路径会落到“default”情况吗? - tresf
默认情况下,只有在第一个情况不成立时,才会处理默认情况。 - Thalia

0
我正在使用一个继承自QApplication的类,当文件准备好时会发出一个信号:
#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将创建的OpenWindowApplication与MainWindow对象连接起来,因此一旦文件准备就绪,信号就会被发射并由其接收以进行处理

#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();
}

MainWindow将fileReady信号连接到一个lambda函数,该函数打开文件并更新小部件

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

这是结果: 4inline

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接