简体   繁体   English

将 Qt 导入现有的 Xcode 项目

[英]Import Qt into an existing Xcode project

I am on MacOS 10.13.3, using Xcode 9 as my IDE, and trying to include Qt 5.10 in my Xcode project.我在 MacOS 10.13.3 上,使用 Xcode 9 作为我的 IDE,并尝试在我的 Xcode 项目中包含 Qt 5.10。 I have installed Qt (with docs and examples) with Homebrew and ran many of them in QtCreator.我已经使用 Homebrew 安装了 Qt(带有文档和示例),并在 QtCreator 中运行了其中的许多。 Everything worked well up to this point.到目前为止,一切都运行良好。

My Xcode project is an extensive, complex project, the development of which has been active for a few years now, so switching to QtCreator or changing the structure of my project is not an option.我的 Xcode 项目是一个广泛而复杂的项目,它的开发已经活跃了几年,所以切换到 QtCreator 或改变我的项目结构不是一个选择。 I really need to add Qt to my existing Xcode project, which IMHO should be quite a natural thing to do given that Qt is a software development framework.我真的需要将 Qt 添加到我现有的 Xcode 项目中,鉴于 Qt 是一个软件开发框架,恕我直言,这应该是很自然的事情。 However I have not been able to accomplish that yet.但是我还没有能够做到这一点。 The upside is that in the process I have been learning quite a bit about Qt and its ecosystem.好的一面是,在这个过程中,我对 Qt 及其生态系统有了很多了解。 I now know how to use qmake, how to set up and successfully compile a project in QtCreator and how to create an Xcode project from QtCreator using qmake.我现在知道如何使用 qmake,如何在 QtCreator 中设置并成功编译项目以及如何使用 qmake 从 QtCreator 创建一个 Xcode 项目。 As I said, I have been running many Qt examples and read pretty much every piece of documentation, blog and SO post I could find about this subject.正如我所说,我一直在运行许多 Qt 示例,并阅读了几乎所有我能找到的关于这个主题的文档、博客和 SO 帖子。 To my surprise I haven't found a direct solution to this problem anywhere on the.net.令我惊讶的是,我在 .net 上的任何地方都没有找到直接解决这个问题的方法。

So far I have included in my Xcode environment QtCore.framework, QtWidgets.framework and QtGui.framework, configured Xcode with the right search paths (Qt finds its files and frameworks) and include the right Qt headers in the code.到目前为止,我已经在我的 Xcode 环境中包含 QtCore.framework、QtWidgets.framework 和 QtGui.framework,使用正确的搜索路径配置 Xcode(Qt 找到它的文件和框架)并在代码中包含正确的 Qt 标头。 However upon building my target I get a series of meaningless compilation errors.然而,在构建我的目标时,我得到了一系列无意义的编译错误。

Basically - if my understanding is correct - I can't just import Qt's components and frameworks I need, and set the right paths in Xcode just like I would do with any other framework.基本上 - 如果我的理解是正确的 - 我不能只导入我需要的 Qt 组件和框架,然后在 Xcode 中设置正确的路径,就像我对任何其他框架所做的那样。 Qt 'features' a preprocessing step using MOC and UIC that sets it apart from other tools. Qt“具有”使用 MOC 和 UIC 的预处理步骤,这使其有别于其他工具。 So I examined the output of qmake and there are two files that the building system produces - qt_makeqmake.mak and qt_preprocess.mak which I think are responsible for the code generation step.因此,我检查了 qmake 的 output,构建系统生成了两个文件——qt_makeqmake.mak 和 qt_preprocess.mak,我认为它们负责代码生成步骤。 Basically this is what I don't know how to translate into my Xcode environment.基本上这是我不知道如何翻译到我的 Xcode 环境中。

Please feel free to tell me if my approach is not correct.如果我的方法不正确,请随时告诉我。 I am open to any advice or suggestion.我愿意接受任何意见或建议。 I would really like to integrate Qt into my project as Qt is such a powerful and complete framework.我真的很想将 Qt 集成到我的项目中,因为 Qt 是如此强大和完整的框架。 I hope someone will be able to help.我希望有人能够提供帮助。 Thank you.谢谢。

1 / In addition to setting the right path to Qt libraries and header files, which is fromwhat you said already done, you need to invoke the moc (Meta-Object Compiler) on your own Qt class which includes the Q_OBJECT macro. 1 /除了已经说过的正确设置Qt库和头文件的路径之外,还需要在自己的Qt类(包括Q_OBJECT宏)上调用moc(元对象编译器)。 (basically every class you have made that are using signal/slot system). (基本上,您使用信号/插槽系统制作的每个类)。

MOC http://doc.qt.io/qt-5/moc.html MOC http://doc.qt.io/qt-5/moc.html

This step must be done BEFORE compiling the project, and the result (the cpp generated moc files) must be compiled AND linked. 必须在编译项目之前完成此步骤,并且必须编译并链接结果(由cpp生成的moc文件)。

Now I am not an expert in XCode and MacOS development but for sure you have a way to add a custom step in your build process for doing that 现在,我不是XCode和MacOS开发方面的专家,但是可以肯定的是,您可以在构建过程中添加自定义步骤来执行此操作

2/ For UIC files : follow approximatively what is explained here (answer of Preetam, not the one validated) to obtain a .cpp and an .h file that you must include in your project too. 2 /对于UIC文件:大致按照此处的说明(Preetam的答案,而不是经过验证的答案)来获得.cpp和.h文件,它们也必须包含在项目中。

Hope this answer will help and point you toward the rigth direction. 希望这个答案能对您有所帮助,并指向正确的方向。

Here is a post explained more in detail what I explained : https://fmilicchio.bitbucket.io/2013/01/xcode-and-qt4-and-qt5/? 这是一个帖子,更详细地解释了我解释的内容: https : //fmilicchio.bitbucket.io/2013/01/xcode-and-qt4-and-qt5/?

would you be kind to share how you have set the right path to Qt libraries and header files.您能否分享一下您如何设置 Qt 库和 header 文件的正确路径。 In my current file in Xcode, I keep getting the "QtWidgets/QApplication' file not found".在我当前的文件 Xcode 中,我一直收到“找不到 QtWidgets/QApplication”文件的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM