@@ -16,26 +16,83 @@ QT_END_NAMESPACE
1616
1717namespace PopMsgBox
1818{
19+ class PopMsgBox : public QObject
20+ {
21+ Q_OBJECT
22+ 23+ QPointer<EventLoop> m_eventLoop{ nullptr };
24+ public:
25+ // static PopMsgBox& instance()
26+ // {
27+ // static PopMsgBox instance;
28+ // return instance;
29+ // }
30+ void enqueueMessage (const QString& t_title, const QString& t_message, const QColor& t_color)
31+ {
32+ if (m_eventLoop) m_eventLoop->enqueueMessage (t_title, t_message, t_color);
33+ }
34+ 35+ void stop () const
36+ {
37+ m_eventLoop->deleteLater ();
38+ }
39+ 40+ // PopMsgBox(const PopMsgBox&) = delete;
41+ // PopMsgBox(PopMsgBox&&) = delete;
42+ // PopMsgBox& operator=(const PopMsgBox&) = delete;
43+ // PopMsgBox& operator=(PopMsgBox&&) = delete;
44+ 45+ PopMsgBox ()
46+ {
47+ const auto popupWindowContainer = new PopupWindowContainer (10000 );
48+ m_eventLoop = new EventLoop (popupWindowContainer);
49+ connect (m_eventLoop, &EventLoop::destroyed, popupWindowContainer, &PopupWindowContainer::deleteLater, Qt::QueuedConnection);
50+ 51+ const auto thread = new QThread;
52+ m_eventLoop->moveToThread (thread);
53+ connect (m_eventLoop, &EventLoop::destroyed, thread, &QThread::quit, Qt::QueuedConnection);
54+ 55+ connect (thread, &QThread::finished, thread, &QThread::deleteLater);
56+ connect (thread, &QThread::finished, this , &PopMsgBox::s_stopped);
57+ 58+ thread->start ();
59+ }
60+ // private:
61+ ~PopMsgBox () override
62+ {
63+ qDebug () << " ~PopMsgBox" ;
64+ if (m_eventLoop) m_eventLoop->deleteLater ();
65+ }
66+ signals:
67+ void s_stopped ();
68+ };
69+ 70+ Q_GLOBAL_STATIC (PopMsgBox, popMsgBox)
1971
2072class MainWindow : public QMainWindow
2173{
2274 Q_OBJECT
23- PopupWindowContainer* m_popupWindowContainer;
24- EventLoop* m_eventLoop{nullptr };
25- int counter{0 };
75+ int m_counter{0 };
76+ bool m_onClosing{ false };
2677public:
2778 MainWindow (QWidget *parent = nullptr );
2879 ~MainWindow () override ;
2980 void closeEvent (QCloseEvent* event) override
3081 {
31- qDebug () << " closeEvent: 1. m_eventLoop" ;
32- m_eventLoop->deleteLater ();
33- connect (m_eventLoop, &EventLoop::destroyed, this , [this ](QObject*)
82+ if (m_onClosing)
3483 {
35- QApplication::quit ();
36- // event->accept();
37- });
84+ event->accept ();
85+ return ;
86+ }
87+ 88+ qDebug () << " closeEvent: 1. m_eventLoop" ;
89+ popMsgBox->stop ();
3890 event->ignore ();
91+ m_onClosing = true ;
92+ connect (popMsgBox, &PopMsgBox::s_stopped, this , &QMainWindow::close,Qt::QueuedConnection);
93+ 94+ hide ();
95+ // connect(popMsgBox, &PopMsgBox::s_stopped, qApp, &QApplication::quit, Qt::QueuedConnection);
3996 }
4097private slots:
4198 void on_pushButton_clicked ();
0 commit comments