cocoa - Qt5。 QMacNativeWidget。体系结构 x86_64 的 undefined symbol

标签 cocoa qt

QMacNativeWidget 在 Qt5 文档中列出,但任何尝试使用嵌入式 Qt Widget 创建 Cocoa 应用程序都会出现下一个错误:

Undefined symbols for architecture x86_64:
  "QMacNativeWidget::QMacNativeWidget(void*)", referenced from:
      -[AppDelegate loadUi] in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是我的代码:

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    std::auto_ptr<QWidget> nativeWidget;
    ...
}
// Qt Widget will be attached to this view
@property (weak) IBOutlet NSView *view; 
@end

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [self initQtIfNeeded];
    [self loadUi];
}

-(void) initQtIfNeeded
{
    QApplication* app = qApp;
    if (app == 0)
    {
        puts("Creating new QApplication instance...");
        QApplication::setDesktopSettingsAware(true);
        QApplication::setAttribute(Qt::AA_MacPluginApplication, true);
        int argc = 0;
        app = new QApplication(argc, NULL);
        ...
    }
}

-(void)loadUi
{
    nativeWidget.reset(new QMacNativeWidget());
    ...

    // Casting pointer (x86_64 with ARC)
    void* winId = reinterpret_cast<void *>(nativeWidget->winId());
    NSView *nativeWidgetNSView = (__bridge NSView*)winId;

    // Attaching Qt Widget to NSView
    [self.view addSubview:nativeWidgetNSView positioned:NSWindowAbove relativeTo:nil];
    nativeWidget->setGeometry(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    ...

    nativeWidget->show();
}

当我使用 QWidget 而不是 QMacNativeWidget(即:nativeWidget.reset(new QWidget());)时,应用程序链接成功,但在运行时创建了附加窗口。(

Screenshot

我的问题:我做错了什么还是Qt问题? 谢谢!

最佳答案

这里有同样的问题。我解决了将文件扩展名从“.cpp”更改为“.mm”的问题。我在一些 QtBug 描述中看到这个技巧,尝试过并且对我有用(使用静态构建的 Qt 5.7)。 .mm 扩展名用于 Objective-C++ 代码(Objective-C 和 C++ 的混合体)。使用 .cpp 扩展名时出现“ undefined symbol ”错误。

关于cocoa - Qt5。 QMacNativeWidget。体系结构 x86_64 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14817278/

相关文章:

Cocoa:获取应用程序可能的插件目录列表的正确方法?

cocoa - 如何防止 NSButton 在禁用时注册按下操作?

linux - ubuntu 11.04 系统无密码关机

qt - 如何优化 QGraphicsView 的性能?

c++ - QGridLayout 没有按预期工作

objective-c - Cocoa - 屏幕捕获和绘图(视网膜)

objective-c - 正确指定非小数 CGFloat 值

objective-c - Cocoa block 变量内存管理

c++ - RegExp 查找命令行参数

c++ - 在 Qt/C++ 中实现撤消功能(如 Ctrl+Z)