gpt4 book ai didi

c++ - 无法在 mac 上使用 qt app 打开文件

转载 作者:太空狗 更新时间:2023-10-29 21:19:35 26 4
gpt4 key购买 nike

我正在尝试将自定义文件与 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 ... 如何 ? 这example看起来不错……但我不明白如何将构造函数和事件结合起来……

我如何使它工作?

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

最佳答案

以下是修改后的代码,它将在启动时或正常运行期间响应 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);
}

关于c++ - 无法在 mac 上使用 qt app 打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26849866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com