1

Qt5文档中列出了QMacNativeWidget类,但是任何尝试使用嵌入式 Qt Widgets 创建 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());应用程序成功链接但在运行时创建了附加窗口。(

截屏

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

4

1 回答 1

0

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

于 2016-12-18T01:26:55.717 回答