来自 C++ 的 NSOpenPanel增强线程

发布于 2024-10-08 05:14:25 字数 1127 浏览 6 评论 0原文

我从 boost C++ 创建的线程调用 NSOpenPanel。

面板行为不稳定并且对鼠标的响应不佳,也就是说,当单击顶级组合框确实提高了响应时,单击对象有时不会执行任何操作。

我必须运行一个单独的运行循环吗?我正在执行一个 runModalForDirectory ,它应该负责运行自己的循环。

我还创建了一个单独的 objc 类,它执行 PerformSelectorOnMainThread 以在主线程中显示面板,但行为仍然相同。

[ps performSelectorOnMainThread:@selector(showOpenPanel) withObject:nil 
                      waitUntilDone:YES
                      modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

我还尝试过 waitUntilDone:NO 并运行 CFRunLoopRunInMode 这也没有帮助。

- (bool) showOpenPanel
{
    NSOpenPanel *op = [NSOpenPanel openPanel];
    [op setAllowsMultipleSelection:YES];
    [op setTitle:@"Choose File"];
    [op setMessage:@"Choose file for Importing."];
    [op setFloatingPanel:true]; 
    bool result =[op runModalForDirectory:NSHomeDirectory() file:nil types:self.fileTypes];
    if (result == NSOKButton) {
        [self setSelectedFiles:[op filenames]];
        [self setLastShowResult:true];
    }
    else {
        [self setLastShowResult:false];
    }

    [self setPanelIsDone:true]; 
    return self.lastShowResult;
}

I'm invoking a NSOpenPanel from a thread created by boost C++.

the panel behaves erratically and doesn't respond well to mouse, that is clicking on objects does nothing sometime when clicking on top level combo box does improve response.

do i've to run a separate runloop I'm doing a runModalForDirectory which should take care of running its own loop.

I've also created a separate objc class which does performSelectorOnMainThread to show panel in main thread but still the behavior is same.

[ps performSelectorOnMainThread:@selector(showOpenPanel) withObject:nil 
                      waitUntilDone:YES
                      modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

I've also tried with waitUntilDone:NO and running a CFRunLoopRunInMode which isn't helping either.

- (bool) showOpenPanel
{
    NSOpenPanel *op = [NSOpenPanel openPanel];
    [op setAllowsMultipleSelection:YES];
    [op setTitle:@"Choose File"];
    [op setMessage:@"Choose file for Importing."];
    [op setFloatingPanel:true]; 
    bool result =[op runModalForDirectory:NSHomeDirectory() file:nil types:self.fileTypes];
    if (result == NSOKButton) {
        [self setSelectedFiles:[op filenames]];
        [self setLastShowResult:true];
    }
    else {
        [self setLastShowResult:false];
    }

    [self setPanelIsDone:true]; 
    return self.lastShowResult;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2024-10-15 05:14:25

NSOpenPanel 是 AppKit 的一部分。 AppKit 函数和类只能在主线程上安全使用。

向我们展示您在 performSelectorOnMainThread 中使用的代码,以便我们帮助找出您可能仍然遇到问题的原因。我怀疑你正在用它调用单独的方法——不要这样做;它不会按照你期望的方式工作。回调主线程以获取与 NSOpenPanel 的全部交互。

NSOpenPanel is part of AppKit. AppKit functions and classes can only be safely used on the main thread.

Show us the code you used with performSelectorOnMainThread so we can help figure out why you might still be seeing problems. I suspect you're calling individual methods with it--don't; it won't work the way you expect. Call back to the main thread for the totality of your interaction with NSOpenPanel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文