Recently, there are several friends using QT to ask Qt for iOS things, because I have very little experience in this area, writing system articles, very sorry, can not give a satisfactory answer, recommend everyone to see Jason's Home, in my blog left sidebar link also has, he provided Qt Some of the most interesting articles for IOS, and are practice-based, and his app is already online in the app Store.
As for me, in this article, a brief introduction to how to mix Qt with OC programming.
What I'm going to say is that most of the QT help is there, please go to indexing mode, type "qt for iOS" to find Qt for iOS article to look at. It introduces three aspects of building development environment , compiling application , mixed OC programming , and has been very detailed.
If you do not want to chew English, then I can read the article down.
Project Settings
Since we're talking about Qt mixed OC programming, let's start with a brief introduction to Objective C. I have only one word: go, ask the search engine to go. Because I know it is limited, I am afraid to mislead you. Of course, if you are not afraid, look down.
Introduction to OC Source files
First of all, I want to say the source file of Objective C, the suffix is. m or. mm, in the. mm file, you can use C + + code directly. So, to mix QT code with OC code, we need to include the mm file in the QT project.
Pro File Configuration
Qt SDK for Mac, after installation, QT Creator compiles the code using the compiled toolchain provided by XCode, compiles the mm file correctly, or links to the library files of IOS.
To mix the OC code, you need to change the pro file. One is to add a mm file, and one is to connect to the library file for IOS.
To add a source file, use the objective_sources variable, such as Jiangzi:
OBJECTIVE_SOURCES += ocview.mm
link library XCode provides libraries, you need to use Qmake_lflags, similar to Jiangzi:
iOS {Qmake_lflags+= -frameworkOpengles Qmake_lflags+= -frameworkGlkit Qmake_lflags+= -frameworkQuartzcore Qmake_lflags+= -frameworkCorevideo Qmake_lflags+= -frameworkCoreAudio qmake_lflags+= -frameworkCoreimage Qmake_lflags+= -frameworkCoreMedia Qmake_lflags+= -frameworkAvfoundation Qmake_lflags+= -frameworkAudiotoolbox Qmake_lflags+= -frameworkCoregraphics Qmake_lflags+= -frameworkUIKit}
Above is the configuration I use Qt for IOS programming. I've used a lot of libraries for IOS, so I've added a lot of frameworks.
The "-framework UIKit" parameter, which is passed to the Clang by Makefile, is a keyword used to indicate that a frame (or library) is to be linked, followed by the frame (library) name.
It is important to note that we use the library for IOS, not the "LIBS + =" way to introduce OH. Of course, you realize it yourself through Qt. A library, you still need to use the "LIBS + =" approach.
Specify the plist file
Sometimes you need to specify a plist file for your project, the full name of the Plist file is the property List, and the suffix is. plist. It is used to define the properties of the iOS app, such as the display name of the bundle (an app on iOS called a bundle), the executable name, the signature, the certificate, and, of course, some configuration data. Refer to the IOS development documentation for a specific introduction.
To add plist to the pro file, use the Qmake_info_plist keyword. As Jiangzi below:
QMAKE_INFO_PLIST += MultiWindow.plist
Well, about the configuration items in the pro file that are related to mixed use of OC. Next we look at how to write Objective C code.
Mix with objective C code
Dear, very frightened, this is my weakness, did not write how many OC code. So, please don't ask me about OC questions, I really don't know ...
Background
My example is to overlay the iOS native interface on the QML interface, i.e. UIView, UIWindow and the like. Because OC is a close relative of C, and C + + has a natural blood, mixed up particularly convenient ha, more than using JNI programming on Android.
But one thing, OC applies [] This syntax to call the function, using XCode, the syntax hints and auto-completion is very powerful, the basic need not think to find the function you want to use. And Qt Creator, hey, there is no such a good phase, purely to handwriting oh. I was looking at the API document with XCode Open, more painful.
What is Qquickview?
I tested the example with the Qt Quick App project template, using Qquickview to load the QML document. This is illustrated here as an example.
The first thing to say is what Qquickview is.
Qquickview, is actually a UIView. UIView is the gener of many interface elements in the IOS development framework, such as UIWindow, which is a subclass of UIView.
Qt Qquickview is a UIView, created Qquickview instance, there is a UIView, then Qt played some magic, got the UIView OpenGL Context, ran the Qt event loop, in this Op The EnGL Context draws its own scene and UI system from scratch.
As simple as this, you can consult the Qt source code to learn more.
It is important to note that the rendering of the QML interface element, and the rendering of the native interface with UIView, are not in one thread. and IOS support for OpenGL ES is good, and you can use multiple OpenGL Context at the same time. Better yet, you can create a OpenGL Context in windowed mode. Unlike Android versions, Qt uses OpenGL in full-screen mode, partial update is not supported, so we use QML's camera and videooutput to develop photo applications on Android, videooutput must be full-screen mode (Must be fill_parent). On IOS, there's no such limit. It seems that IOS is still beautiful.
I'm a bit far behind, and I've been writing less technical articles lately, and I'm getting wordy. Let's get down to it.
Because Qquickview is actually a UIView, you can cast to UIView and then use the OC method to create a new UIView or UIWindow, so that you have the native UI component, and you can use the Op on this native UI component. It's nice to EnGL to draw your own stuff or add other native controls.
However, it is to be explained that the IOS native interface elements created by this method will always cover the interface elements in the QML scene on top of the QML interface.
Mixed code
To use OC's class library, you need to include the relevant header file in the mm file, and a few parts of the work to do. One is to add the SDK path in the pro file, use the Includepath variable, not much to say. Another point is the mm file contains OC header files, and C + + header file a reason, but to use the #import Oh. Similar to Jiangzi:
#import <UIKit/UIKit.h>#import <GLKit/GLKit.h>
With the header file, you can use the OC class library. For example, I want to create a new IOS native UIView on Qquickview, which can be done in. mm Files:
void addOCView(QQuickWindow *w){ reinterpret_cast<UIView *>(w->winId()); CGRect viewRect = CGRectMake(1010100100); UIView* myView = [[UIView alloc] initWithFrame:viewRect]; [myView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]]; [view addSubview: myView];}
As you can see, I wrote a Addocview method whose parameters are Qquickview. In the Addocview method, I cast the qquickview to UIView to use.
I created a new UIView, set its background color, and then added it as a qquickview subwindow. Connaught, it's so easy.
Next main.cpp, see how it uses the Addocview () method. The code is as follows:
int main(int*argv[]){ QGuiApplication app(argc, argv); QQuickView viewer; viewer.setResizeMode(QQuickView::SizeRootObjectToView); viewer.setSource(QUrl("qrc:/main.qml")); viewer.show(); addOCView(&viewer); return app.exec();}
Not a bit of surprise, it is directly called Addocview Oh. Haha, that's true.
Location mapping of IOS native interface and QML elements
When mixed with the IOS native interface, you can also achieve a seamless integration of the native interface with the QML interface. The key is to calculate the location of the QML interface elements, and then set the location of the native interface based on the location of the QML interface elements.
QML element Position Conversion
QML provides a way to convert the position of an element, Item has a method called Maptoitem (), it can map a region relative to the Qt Quick item to another item, get the coordinates. For example, your qml file looks like this:
Rectangle { ... Rectangle { id: videoLayer; 8; anchors.left: parent.left; anchors.right: parent.right; anchors.top: parent.top; anchors.bottom: actionBar.top; "green"; } ...}
You want to position the native UIView inside an element with ID Videolayer, you can convert a coordinate like this and use it:
var coordinate = videoLayer.mapToItem88, videoLayer.width16, videoLayer.height16);winUtil.addUIView(coordinate.x, coordinate.y, coordinate.width, coordinate.height);
The converted coordinate have the x, y, Width, height properties. When the first argument of Maptoitem is null, the result of the conversion is relative to Qquickview. When we add UIView, we can use this conversion result to construct a CGRect object, using this cgrect to initialize the UIView position.
Setting the location of the UIView
The Winutil in the previous sample code is an auxiliary class that I implemented in C + + and exported to the QML environment. Its Adduiview method sets the location of the native UIView based on the coordinates that are passed in. The reference code is as follows:
reinterpret_cast<UIView*>(view->winId()); uiw = [[UIWindow alloc] initWithFrame:CGRectMake(x, y, width, height)]; [v addSubview: uiw];
In the above code, view is Qquickview. This time we created UIView using the incoming x, y, width, height, so that the new UIView and QML elements are integrated together, it looks as if it is a whole.
OK, that's all.
Qt for IOS,QT and objective C mixed programming