-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
71 lines (63 loc) · 1.86 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
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#ifdef __ANDROID__
#include <jni.h>
#endif
#include <QDebug>
#include "CrossQFile.h"
bool waitForActivityResult{false};
QString selectFileUri(){
#ifdef __ANDROID__
waitForActivityResult = true;
QAndroidJniObject::callStaticMethod<void>("crossqfile/MainActivity", "selectFile");
while(waitForActivityResult){
QCoreApplication::processEvents();
}
return QAndroidJniObject::callStaticObjectMethod("crossqfile/MainActivity", "getSelectedUri","()Ljava/lang/String;").toString();
#else
return "D:\\myfile.txt";
#endif
}
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QString uri{selectFileUri()};
#ifdef __ANDROID__
CrossQFile file{uri,true};
#else
CrossQFile file{uri};
#endif
qDebug() << "File name: " << file.fileName();
qDebug() << "File display name: " << file.displayName();
qDebug() << "File size: " << file.size();
qDebug() << "File exists: " << file.exists();
if(file.open(CrossQFile::WriteOnly)){
file.write("Hello CrossQFile!");
file.close();
}else{
qDebug() << "Could not open the file";
}
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
#ifdef __ANDROID__
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_crossqfile_MainActivity_stopWaiting(JNIEnv* env, jobject thisObject){
waitForActivityResult = false;
}
#ifdef __cplusplus
}
#endif
#endif