类 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问题? 谢谢!
答案 0 :(得分:0)
这里的问题相同。我解决了将文件扩展名从“.cpp”更改为“.mm”的问题。我在一些QtBug描述中看到了这个技巧,尝试并为我工作(使用静态构建的Qt 5.7)。 .mm扩展用于Objective-C ++代码,Objective-C和C ++的混合。使用.cpp扩展名我有“Undefined Symbols”错误。