URL scheme - Qt和mac [英] URL scheme - Qt and mac

查看:900
本文介绍了URL scheme - Qt和mac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程式导入自订网址方案。我为Info.plist添加了必要的行。在调用指定的URL(例如:myapp://)后,应用程序启动。

I'm trying to implement a custom URL scheme for my application. I've added the necessary lines for my Info.plist. After calling the specified url (eg.: myapp://) the application launches.

如果我想处理URL,我发现这些步骤: p>

If I want to handle the URL, I've found these steps:

@interface EventHandler : NSObject {
}
@end

@implementation EventHandler
- (id)init {
    self = [super init];

    if (self) {
        NSLog(@"eventHandler::init");

        NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self
                        selector:@selector(applicationDidFinishLaunching:)
//                        name:NSApplicationWillFinishLaunchingNotification
          name:NSApplicationDidFinishLaunchingNotification
                        object:nil];
    }
    return self;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSLog(@"%@", url);
}

@end

应用程序正在运行,但如果URL被调用并且应用程序已终止,则不会捕获该事件。我想这是因为这:NSApplicationDidFinishLaunchingNotification。
将其更改为NSApplicationWillFinishLaunchingNotification会导致捕获非事件。也许Qt在我之前处理,但我找不到解决方法的问题。

The above code is working if the application is running, but if the URL gets called and the application was terminated, the event is not caught. I think this is because this: NSApplicationDidFinishLaunchingNotification. Changing it to NSApplicationWillFinishLaunchingNotification causes that non events caught. Maybe Qt handles it before me, but I can't find a workaround for the problem.

推荐答案

我的基于Qt的应用程序处理在Mac上的自定义URL方案,并沿着与原始海报相同的路径。事实证明,Qt4已经支持Mac上的URL事件,没有必要编写Objective-C代码来接收它们。这实际上是你在响应NSApplicationWillFinishLaunchingNotification设置事件处理程序时没有收到任何URL事件的原因:Qt注册自己的处理程序。

I was also trying to get my Qt-based application handle a custom URL scheme on the Mac and went down the same path as the original poster. It turns out that Qt4 already supports URL events on the Mac, and there's no need to write Objective-C code to receive them. This is in fact the reason that you didn't receive any URL events when you set the event handler in response to NSApplicationWillFinishLaunchingNotification: Qt registers its own handler afterward.

当URL与您的自定义方案被触发,您的Qt应用程序将收到一个FileOpenEvent。注意,它是接收事件的QApplication实例。您可以通过使应用程序子类化QApplication或在标准QApplication上安装事件过滤器来捕获它。我选择了第二种方法。

When a URL with your custom scheme is triggered, your Qt application will receive a FileOpenEvent. Note that it is the QApplication instance which receives the event. You can catch it by making your application subclass QApplication or by installing an event filter on the standard QApplication. I opted for this second approach.

下面是我的自定义事件过滤器类FileOpenEventFilter的eventFilter方法。它只是在事件包含非空URL时发出信号urlOpened。它还保存最后打开的网址,以防我的主窗口在事件到达时未完全初始化(当我的应用程序中,当点击自定义网址时它还没有运行)。

Here's the eventFilter method of my custom event filter class, FileOpenEventFilter. It just emits the signal urlOpened when the event contains a non-empty URL. It also saves the last opened URL in case my main window isn't completely initialized when the event arrives (which happens in my app when it's not already running when the custom URL is clicked.)

bool FileOpenEventFilter::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::FileOpen)
    {
        QFileOpenEvent* fileEvent = static_cast<QFileOpenEvent*>(event);
        if (!fileEvent->url().isEmpty())
        {
            m_lastUrl = fileEvent->url().toString();
            emit urlOpened(m_lastUrl);
        }
        else if (!fileEvent->file().isEmpty())
        {
            emit fileOpened(fileEvent->file());
        }

        return false;
    }
    else
    {
        // standard event processing
        return QObject::eventFilter(obj, event);
    }
}

这篇关于URL scheme - Qt和mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆