最近公司开了一个新的项目是要求跨平台开发的,所以采用的是QT开发,但是由于QT使用的是QWidget开发的界面效果肯定不尽人意,所以使用的是iOS和QT混合开发,即:当在工程的主入口的位置直接由iOS的UIWindow直接接手,中间有特别需要的就加载QT的界面,说白了就是整个工程基本是OC搭建界面,个别的界面需要加载QWidget的界面.为了能让各位更明白我可能说的比较细,前面的部分大家可以选择性的查看
(一)前提要求(准备)
必须有两样东西(据说他们的版本需要对应不太清楚,所以在这里贴一下)(补充测试了一下版本不对应也没事的)
(1)Xcode(我的版本是9.2)
(2)Qt Creator(我的版本是4.5.1)
对于他们的下载方式
Xcode的话只需要到苹果的AppStore就可以下载
QT的话从官网下载可能会很慢可能需要用到VPN,我这里有一个国内的开源镜像,大家可以去下载网速还行[点击进入](安装的话就按部就班的就行如果不会的话可以留言)
(二)进入正题
1.在QT中创建工程
2.接下来的操作就是完全在Xcode中操作了,把qt关了吧省省内存哈哈
(1)通过路径找到文件双击打开Xcode工程
(2)进去后应该是这个样子的
#include "mywidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
return a.exec();
}
(3)接下来使用快捷键command+N创建一个类
(4)创建完成后把下面的代码全部复制进去(差点忘了把QIOSApplicationDelegate的后缀改成.mm必须改否则他会报错,报到让你怀疑人生)
!!!QIOSApplicationDelegate.h的代码
#import <UIKit/UIKit.h>
@interface QIOSApplicationDelegate : UIResponder<UIApplicationDelegate>
@property (strong, nonatomic) QUIWindow *window;
@end
!!!QIOSApplicationDelegate.mm的代码
#import "QIOSApplicationDelegate.h"
#import "MainViewController.h"
#include <QWindow>
@interface QIOSApplicationDelegate ()
@end
@implementation QIOSApplicationDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// _qApp = new QApplication(_argc, _argv);
self.window = [[QUIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[MainViewController alloc]init];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
(5)这个时候你肯定发现报错了不着急慢慢来创建下面的两个类创建完了你在Command+B编译一下发现就会瞬间没错了
MainViewController.m的代码
#import "MainViewController.h"
#import "QIOSViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
// Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
QIOSViewController * my = [[QIOSViewController alloc]init];
[self presentViewController:my animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
(6)接下来创建一个很关键的类(都是坑啊都得按照QT的规矩来)
这下面的两段代码必须写进QIOSViewController.mm别问我为什么,我特么也不知道
- (id)initWithQIOSScreen:(UIScreen*)scrren{
return self;
}
- (void)lockedOrientation{
}
接下来就是QIOSViewController.mm的代码了
#import "QIOSViewController.h"
#include "mywidget.h"
#import "QIOSApplicationDelegate.h"
@interface QIOSViewController ()
@end
static MyWidget * w;
@implementation QIOSViewController
{
UIView * m_sview;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addview];
self.view.backgroundColor = [UIColor redColor];
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];
btn.backgroundColor = [UIColor blueColor];
[btn addTarget:self action:@selector(btn) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:btn];
UIButton * btnR = [[UIButton alloc]initWithFrame:CGRectMake(200, 0, 100, 300)];
btnR.backgroundColor = [UIColor blueColor];
[btnR addTarget:self action:@selector(btnR) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:btnR];
// Do any additional setup after loading the view.
}
- (void)btnR{
m_sview.hidden = YES;
[m_sview removeFromSuperview];
m_sview == nil;
// [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)btn{
[self addview];
UIView* newView;
if (!w) {
w = new MyWidget();
}
newView = (__bridge UIView*)reinterpret_cast<void*>(w->winId());
[m_sview addSubview:newView];
// QWindow * newview1 = w->windowHandle();
w->show();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
// delete w;
// w->close();
});
});
}
- (void)addview{
// if (!view) {
[m_sview removeFromSuperview];
m_sview == nil;
m_sview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-300)];
m_sview.center = self.view.center;
m_sview.backgroundColor = [UIColor yellowColor];
[self.view addSubview:m_sview];
// }
}
- (id)initWithQIOSScreen:(UIScreen*)scrren{
return self;
}
- (void)lockedOrientation{
}
- (void)viewWillDisappear:(BOOL)animated{
}
- (void)viewWillAppear:(BOOL)animated{
}
//- (void)initWithQIOSScreen:(QIOSScreen*)screen{
//
//}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"点击了");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
(7)现在大家可以跑一遍xcode工程基本可以跑通了,今天太晚了就先写到这里明天再跟大家说一下细节,以及里面遇到的坑,真心坑的要死!!!
书写不易,纯手写喜欢的关注一下,有问题可留言
/
/
/