1

クラスQMacNativeWidgetは Qt5 ドキュメントにリストされていますが、Qt ウィジェットが埋め込まれた 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();
}

QMacNativeWidget (つまり:) の代わりに QWidget を使用している場合、nativeWidget.reset(new QWidget());アプリケーションは正常にリンクされますが、実行時に追加のウィンドウが作成されます。(

スクリーンショット

私の質問: 私は何か間違ったことをしていますか、それとも Qt の問題ですか? ありがとう!

4

1 に答える 1

0

ここでも同じ問題。ファイル拡張子を「.cpp」から「.mm」に変更する問題を解決しました。このトリックは QtBug の説明で見られ、試してみてうまくいきました (静的にビルドされた Qt 5.7 を使用)。.mm 拡張子は、Objective-C と C++ が混在する Objective-C++ コードで使用されます。.cpp 拡張子を使用すると、「未定義のシンボル」エラーが発生します。

于 2016-12-18T01:26:55.717 に答える