forked from GDPURJYFS/WellChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
75 lines (57 loc) · 2.04 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*!
* Activity 先于 Qt 加载
* 1. 在 Activity OnCreate 中调用 QtBridgingAndroid::Init,然后进入Qt::main
* 2. 在 Qt::main 中注册 Java 的 native 函数 QtBridgingAndroid::notifiedKeyboardRectangle
* 3. 在 Qt::main 通过调用 Java::QtBridgingAndroid::listenKeyboardHeight 注入监听键盘事件
* 4. 在 Qt::main 加载 QML。
*/
#include <QApplication>
#include <QQmlApplicationEngine>
#include <qqml.h>
#include <QQmlContext>
#include "Sparrow/qtbridgingandroid.h"
#include "Sparrow/keyboard.h"
#include "src/wellchat/collectionsmodel.h"
int main(int argc, char *argv[])
{
//! [java register native function]
#ifdef Q_OS_ANDROID
qDebug() << "QtNative::registerNativeMethod : "
<< QtBridgingAndroid::registerNativeMethodForJava();
#endif
//! [java register native function]
QApplication app(argc, argv);
//! [0]
app.setApplicationName("WellChat");
app.setOrganizationDomain("github.com/GDPURJYFS");
app.setOrganizationName("GDPURJYFS");
app.setApplicationVersion("0.0.1");
//! [0]
QQmlApplicationEngine engine;
//! [2] register qml type
qmlRegisterType<CollectionsModel>("WellChat", 1, 0, "CollectionsModel");
//! [2]
//! [3]
//! import path or imoprt plugin
engine.addImportPath("qrc:/qml/WellChat");
//! [3]
//! [4]
//! load qml file
engine.load(QUrl(QStringLiteral("qrc:/qml/WellChat/main.qml")));
//! [4]
//! [5]
QtBridgingAndroid *BridgingAndroid = new QtBridgingAndroid(&engine);
QQmlContext *context = engine.rootContext();
context->setContextProperty("BridgingAndroid", BridgingAndroid);
#ifdef Q_OS_ANDROID
//! [1] 向java安装事件监听,需要在 QApplication 示例化之后
QtBridgingAndroid::installListener();
// QQmlEngine: Illegal attempt to connect to Keyboard(0xe20036a0)
// that is in a different thread than the QML engine
// QQmlApplicationEngine(0xe0fa1924.
//! [1]
#endif
context->setContextProperty("Keyboard", Keyboard::singleton());
//! [5]
return app.exec();
}