diff --git a/HotkeyTest/hottestwidget.cpp b/HotkeyTest/hottestwidget.cpp index d3e33d2..292f6f9 100644 --- a/HotkeyTest/hottestwidget.cpp +++ b/HotkeyTest/hottestwidget.cpp @@ -7,11 +7,15 @@ HotTestWidget::HotTestWidget(QWidget *parent) : hotkey_1(new QHotkey(this)), hotkey_2(new QHotkey(this)), hotkey_3(new QHotkey(this)), - hotkey_4(new QHotkey(this)), - hotkey_5(new QHotkey(this)), + hotkey_4(new QHotkey(NULL)), + hotkey_5(new QHotkey(NULL)), + thread4(new QThread(this)), + thread5(new QThread(this)), testHotkeys() { ui->setupUi(this); + this->thread4->start(); + this->thread5->start(); //1 connect(this->ui->hotkeyCheckbox_1, &QCheckBox::toggled, @@ -91,6 +95,11 @@ HotTestWidget::HotTestWidget(QWidget *parent) : HotTestWidget::~HotTestWidget() { + this->thread4->quit(); + this->thread4->wait(); + this->thread5->quit(); + this->thread5->wait(); + delete ui; } @@ -174,3 +183,23 @@ void HotTestWidget::on_groupBox_toggled(bool checked) for(QHotkey *hotkey : this->testHotkeys) hotkey->setRegistered(checked); } + +void HotTestWidget::on_threadEnableCheckBox_clicked(bool checked) +{ + this->ui->hotkeyCheckbox_1->setChecked(false); + this->ui->hotkeyCheckbox_2->setChecked(false); + this->ui->hotkeyCheckbox_3->setChecked(false); + this->ui->hotkeyCheckbox_4->setChecked(false); + this->ui->hotkeyCheckbox_5->setChecked(false); + + Q_ASSERT(!this->hotkey_4->isRegistered()); + Q_ASSERT(!this->hotkey_5->isRegistered()); + + if(checked) { + this->hotkey_4->moveToThread(this->thread4); + this->hotkey_5->moveToThread(this->thread5); + } else { + this->hotkey_4->moveToThread(this->thread()); + this->hotkey_5->moveToThread(this->thread()); + } +} diff --git a/HotkeyTest/hottestwidget.h b/HotkeyTest/hottestwidget.h index 3b9d6c3..f9161ce 100644 --- a/HotkeyTest/hottestwidget.h +++ b/HotkeyTest/hottestwidget.h @@ -3,6 +3,7 @@ #include #include +#include namespace Ui { class HotTestWidget; @@ -37,6 +38,8 @@ private slots: void on_groupBox_toggled(bool checked); + void on_threadEnableCheckBox_clicked(bool checked); + private: Ui::HotTestWidget *ui; @@ -46,6 +49,9 @@ private: QHotkey *hotkey_4; QHotkey *hotkey_5; + QThread *thread4; + QThread *thread5; + QList testHotkeys; }; diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui index 5bd93ac..c12140a 100644 --- a/HotkeyTest/hottestwidget.ui +++ b/HotkeyTest/hottestwidget.ui @@ -916,6 +916,68 @@ + + + Threading + + + + 12 + + + + + <html><head/><body><p>Use this test to determine whether shortcuts on multiple threads are supported or not. Internally, the backend of the QHotkey class is <span style=" font-weight:600;">threadsafe</span>, and thus QHotkey is reentrant and can be used on any thread. However, some operating systems might not support creating hotkeys from different threads. </p><p>To enable threading, check the box below and return to the playground. <span style=" font-style:italic;">Hotkey 4 and 5</span> will now each run on their own thread. </p></body></html> + + + Qt::RichText + + + true + + + + + + + + 75 + true + + + + Enable Threaded Hotkeys + + + + + + + <html><head/><body><p><span style=" font-weight:600;">Results so far:</span></p><ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" text-decoration: underline;">Windows:</span> Registering on multiple threads is possible, but: The thread that registered must unregister too and events will be send to the thread registered from, not the main thread</li></ul></body></html> + + + Qt::RichText + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + +