Ref T486 Using QMetaObject::invokeMethod with pointer-to-member-function (or functor).

This commit is contained in:
Mat Sutcliffe
2018-12-25 14:27:20 +00:00
parent f48a411f43
commit bd9948bbff
7 changed files with 15 additions and 13 deletions

View File

@@ -44,6 +44,7 @@ namespace BlackMisc
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
{
const CStatusMessage statusMessage(type, context, message);
const auto invokee = [statusMessage] { CLogHandler::instance()->logLocalMessage(statusMessage); };
#if defined(Q_CC_MSVC) && defined(QT_NO_DEBUG)
if (type == QtFatalMsg) { MessageBoxW(nullptr, message.toStdWString().c_str(), nullptr, MB_OK); }
#endif
@@ -51,10 +52,10 @@ namespace BlackMisc
{
// Fatal message means this thread is about to crash the application. A queued connection would be useless.
// Blocking queued connection means we pause this thread just long enough to let the main thread handle the message.
QMetaObject::invokeMethod(CLogHandler::instance(), "logLocalMessage", Qt::BlockingQueuedConnection, Q_ARG(BlackMisc::CStatusMessage, statusMessage));
QMetaObject::invokeMethod(CLogHandler::instance(), invokee, Qt::BlockingQueuedConnection);
return;
}
QMetaObject::invokeMethod(CLogHandler::instance(), "logLocalMessage", Q_ARG(BlackMisc::CStatusMessage, statusMessage));
QMetaObject::invokeMethod(CLogHandler::instance(), invokee);
}
void CLogHandler::install(bool skipIfAlreadyInstalled)

View File

@@ -73,7 +73,7 @@ namespace BlackMisc
worker->setObjectName(name);
worker->moveToThread(thread);
const bool s = QMetaObject::invokeMethod(worker, "ps_runTask");
const bool s = QMetaObject::invokeMethod(worker, &CWorker::ps_runTask);
Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke");
Q_UNUSED(s);
thread->start();

View File

@@ -54,7 +54,7 @@ namespace BlackMisc
template <typename F>
void singleShot(int msec, QObject *target, F &&task)
{
QSharedPointer<QTimer> timer(new QTimer, [](QObject * o) { QMetaObject::invokeMethod(o, "deleteLater"); });
QSharedPointer<QTimer> timer(new QTimer, [](QObject * o) { QMetaObject::invokeMethod(o, &QObject::deleteLater); });
timer->setSingleShot(true);
timer->moveToThread(target->thread());
QObject::connect(timer.data(), &QTimer::timeout, target, [trace = getStackTrace(), task = std::forward<F>(task), timer]() mutable
@@ -63,7 +63,7 @@ namespace BlackMisc
timer.clear();
task();
});
QMetaObject::invokeMethod(timer.data(), "start", Q_ARG(int, msec));
QMetaObject::invokeMethod(timer.data(), [t = timer.data(), msec] { t->start(msec); });
}
//! @}