Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5de0c5e

Browse files
dev: made correct threads closing
1 parent b96fa94 commit 5de0c5e

File tree

3 files changed

+72
-26
lines changed

3 files changed

+72
-26
lines changed

‎MainWindow.cpp‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@
44

55
PopMsgBox::MainWindow::MainWindow(QWidget *parent)
66
: QMainWindow(parent)
7-
, ui(new Ui::MainWindow),
8-
m_popupWindowContainer(new PopupWindowContainer(10000))
7+
, ui(new Ui::MainWindow)
98
{
10-
m_eventLoop = new EventLoop(m_popupWindowContainer);
11-
//experimental>>>>>>>>>>>>>>>>>>>>>>>>>>>
12-
QThread* t = new QThread;
13-
m_eventLoop->moveToThread(t);
14-
t->start();
15-
connect(m_eventLoop, &EventLoop::destroyed, t, [t]() {t->quit(); t->deleteLater(); }, Qt::QueuedConnection);
16-
connect(m_eventLoop, &EventLoop::destroyed, m_popupWindowContainer, &PopupWindowContainer::deleteLater, Qt::QueuedConnection);
17-
//<<<<<<<<<<<<<<<<<<<<<<<<<<<experimental
189
ui->setupUi(this);
1910

2011
auto setButtonColor = [this](const QColor& t_color){
@@ -32,7 +23,7 @@ PopMsgBox::MainWindow::MainWindow(QWidget *parent)
3223
//info
3324
connect(ui->infoButton, &QPushButton::clicked, this, [this](){
3425
//m_popupWindowContainer.pushMessage(ui->textEdit_2->toPlainText(), ui->textEdit->toPlainText(), PopupWindowContainer::MessageType::Info);
35-
m_eventLoop->deleteLater();
26+
//m_eventLoop->deleteLater();
3627
});
3728

3829
//warning
@@ -42,21 +33,18 @@ PopMsgBox::MainWindow::MainWindow(QWidget *parent)
4233

4334
//error
4435
connect(ui->errorButton, &QPushButton::clicked, this, [this](){
45-
//m_popupWindowContainer.pushMessage(ui->textEdit_2->toPlainText(), ui->textEdit->toPlainText(), PopupWindowContainer::MessageType::Error);
36+
//popMsgBox->enqueueMessage(ui->textEdit_2->toPlainText(), ui->textEdit->toPlainText(), PopupWindowContainer::MessageType::Error);
4637
});
4738
}
4839

4940
PopMsgBox::MainWindow::~MainWindow()
5041
{
5142
delete ui;
5243
qDebug() << "~MainWindow()";
53-
//m_popupWindowContainer.deleteLater();
5444
}
5545

5646
void PopMsgBox::MainWindow::on_pushButton_clicked()
5747
{
58-
//m_popupWindowContainer.pushMessage(ui->textEdit_2->toPlainText(), ui->textEdit->toPlainText(), m_color);
59-
counter++;
60-
//m_popupWindowContainer.pushMessage(ui->textEdit_2->toPlainText() + " " + QString::number(counter), ui->textEdit->toPlainText(), m_color);
61-
m_eventLoop->enqueueMessage(ui->textEdit_2->toPlainText() + " " + QString::number(counter), ui->textEdit->toPlainText(), m_color);
48+
m_counter++;
49+
popMsgBox->enqueueMessage(ui->textEdit_2->toPlainText() + " " + QString::number(m_counter), ui->textEdit->toPlainText(), m_color);
6250
}

‎MainWindow.h‎

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,83 @@ QT_END_NAMESPACE
1616

1717
namespace 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

2072
class 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 };
2677
public:
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
}
4097
private slots:
4198
void on_pushButton_clicked();

‎PopupWindowContainer.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ PopMsgBox::PopupWindowContainer::~PopupWindowContainer()
1616
{
1717
for (const auto& e : m_popupWindows)
1818
{
19+
e->disconnect();
1920
e->deleteLater();
2021
}
2122

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /