https://github.com/recp/qt-dispatch
https://github.com/recp/qt-dispatch
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/recp/qt-dispatch
- Owner: recp
- License: mit
- Created: 2013-09-09T21:28:40.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-12-15T09:59:18.000Z (about 12 years ago)
- Last Synced: 2025-01-28T15:19:58.160Z (12 months ago)
- Language: C++
- Size: 125 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Qt-Dispatch
===========
Execute block on the main thread or specified thread with Qt framework.
No required SIGNAL/SLOTs, no required QObject::connect...
Use with functions or C++11 Lamdas
Functions
------------
```cpp
static void q_dispatch_async_main(fnBlock block); // Execute on Main / UI thread.
static void q_dispatch_async(QThread* thread, fnBlock block); // Execute on some thread.
```
SAMPLE USAGE
------------
```cpp
#include "qt_dispatch.h"
// Usage 1: Using with lambdas
// This function called from non-ui/background thread.
void MainWindow::handleSockData(void *obj) {
// This usage requires C++11
q_dispatch_async_main([&]() {
// Add widgets to window using ui object.
SomeWidget *widget = new SomeWidget();
ui->someVLayout->addWidget(widget);
// check the thread id
const bool isGuiThread = QThread::currentThread() ==
QCoreApplication::instance()->thread();
qWarning() << "isGuiThread: " << isGuiThread; // true
});
}
// Usage 2: Using with functions
void MainWindow::handleSockData(void *obj{
void somefunc();
q_dispatch_async_main(somefunc);
}
void somefunc() {
const bool isGuiThread = QThread::currentThread() ==
QCoreApplication::instance()->thread();
qWarning() << "isGuiThread: " << isGuiThread; // true
}
// Usage 3: Using with other threads
void MainWindow::on_action1_triggered() {
QThread *newThread = new QThread();
newThread->start();
q_dispatch_async(newThread, []() {
const bool isGuiThread = QThread::currentThread() ==
QCoreApplication::instance()->thread();
qWarning() << "isGuiThread: " << isGuiThread; // false
});
}
```
Enabling C++11 Compiler Support: Add these lines to your .pro file
```
CONFIG += c++11
QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.7
LIBS += -stdlib=libc++ -mmacosx-version-min=10.7
```
###Enjoy!