fixup showMessage from both qt thread and external thread, while blocking

This commit is contained in:
Joakim L. Gilje
2021-11-29 17:25:31 +01:00
parent 238fb7ef62
commit 8385051f6a
2 changed files with 18 additions and 5 deletions

View File

@@ -12,6 +12,7 @@ extern "C" {
#include <QWindow>
#include <QDebug>
#include <QTimer>
#include <QThread>
#include <QKeyEvent>
#include <QMessageBox>
@@ -25,10 +26,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui->setupUi(this);
connect(this, &MainWindow::showMessage, this, [this](const QString& header, const QString& message) {
QMessageBox box(QMessageBox::Warning, header, message, QMessageBox::NoButton, this);
box.exec();
}, Qt::BlockingQueuedConnection);
connect(this, &MainWindow::showMessageForNonQtThread, this, &MainWindow::showMessage_, Qt::BlockingQueuedConnection);
connect(this, &MainWindow::pollMouse, this, [] {
sdl_mouse_poll();
@@ -288,3 +286,16 @@ static const uint16_t xfree86_keycode_table[keycode_entries] = {
void MainWindow::on_actionFullscreen_triggered() {
setFullscreen(true);
}
void MainWindow::showMessage(const QString& header, const QString& message) {
if (QThread::currentThread() == this->thread()) {
showMessage_(header, message);
} else {
emit showMessageForNonQtThread(header, message);
}
}
void MainWindow::showMessage_(const QString &header, const QString &message) {
QMessageBox box(QMessageBox::Warning, header, message, QMessageBox::NoButton, this);
box.exec();
}

View File

@@ -15,6 +15,8 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void showMessage(const QString& header, const QString& message);
signals:
void paint(const QImage& image);
void resizeContents(int w, int h);
@@ -25,8 +27,8 @@ signals:
void setFullscreen(bool state);
void setMouseCapture(bool state);
void showMessage(const QString& header, const QString& message);
void showMessageForNonQtThread(const QString& header, const QString& message);
private slots:
void on_actionFullscreen_triggered();
void on_actionSettings_triggered();