Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (86 loc) · 3.02 KB
/
Copy pathmain.cpp
File metadata and controls
99 lines (86 loc) · 3.02 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "mainwindow.h"
#include <QApplication>
#include <QSslSocket>
#include <QSplashScreen>
#include <QScreen>
#include <iostream>
int nMain(int argc, char *argv[])
{
// QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication a(argc, argv);
//注册
qRegisterMetaType<QList<device>>();
qRegisterMetaType<BusinessLogic::BusinessEvent>();
qRegisterMetaType<TransmissionEngine::SendInfo>();
QPixmap pixmap;pixmap.load(":/rc/img/splash1.png");
pixmap=pixmap.scaledToHeight(QApplication::primaryScreen()->size().height()/2,Qt::SmoothTransformation);
QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
splash.show();
for(int i=0;i<10;i++){
splash.raise();
a.processEvents();
splash.show();
a.processEvents();
splash.showNormal();
a.processEvents();
}
//创建对象
QThread workThread;
BusinessLogic *bl = nullptr;
// RUN_IN_CUSTOM_THREAD(&workThread,new BusinessLogic(nullptr););
QObject *tmp = new QObject();
tmp->moveToThread(&workThread);
// bl->moveToThread(&workThread);
workThread.start();
// RUN_IN_CUSTOM_THREAD(&bl,bl.init());
QMetaObject::invokeMethod(tmp,[&bl]{bl=new BusinessLogic;},Qt::BlockingQueuedConnection);
QMetaObject::invokeMethod(bl,[=]{bl->init();},Qt::QueuedConnection);
ViewModel *vm=new ViewModel(bl);
MainWindow *w=new MainWindow(vm,nullptr,[&](QString a){splash.showMessage(a,Qt::AlignBottom|Qt::AlignHCenter);});
// w->show();
// splash.finish(w);
{
QObject obj;//用于自动销毁连接
vm->o_status.use(&obj,[&splash,vm]{splash.showMessage(vm->o_status.get(),Qt::AlignHCenter|Qt::AlignBottom);});
QEventLoop loop;
QObject::connect(bl,&BusinessLogic::businessEventOccurred,&obj,[&loop](BusinessLogic::BusinessEvent event,[[maybe_unused]]QVariantMap m){
if(event==BusinessLogic::BusinessEvent::LoadedSuccessfully){
loop.quit();
}
});
loop.exec();
}
w->show();
splash.close();
//线程检测
bool isMainThread = false;
QMetaObject::invokeMethod(bl,[&]{isMainThread=bl->thread()->isMainThread();},Qt::BlockingQueuedConnection);
if(isMainThread){
ncritical<<"BusinessLogic runs in main thread";
}
int result = a.exec();
w->deleteLater();
vm->deleteLater();
tmp->deleteLater();
// RUN_IN_CUSTOM_THREAD(bl,bl->destory(););
QMetaObject::invokeMethod(bl,[=]{bl->destory();},Qt::BlockingQueuedConnection);
QMetaObject::invokeMethod(bl,[=]{bl->deleteLater();},Qt::BlockingQueuedConnection);
workThread.quit();
workThread.wait(5000);
if(!workThread.isFinished()){
workThread.terminate();
}
return result;
}
int main(int argc,char *argv[]){
if(sodium_init()==-1){
std::cerr<<"Sodium init failed!"<<std::endl;
return 1;
}
do{
int ret=nMain(argc,argv);
if(ret!=EXIT_CODE_RESTART){
return ret;
}
}while(1);
}