diff options
author | Alex Merry <alex.merry@kde.org> | 2015-11-08 23:10:59 +0000 |
---|---|---|
committer | Alex Merry <alex.merry@kde.org> | 2015-11-30 14:07:07 +0000 |
commit | fb4d91996affdbcd3ffc19c7646fbf77b96bf1e5 (patch) | |
tree | 2f66eec33d3b7b96691fe5aa5f9fe96f2029229f /tests/ECMPoQmToolsTest/tr_thread_test.cpp | |
parent | 1e8e0da3eb475bb8b78baa54cb0c34b913c2dc5d (diff) | |
download | extra-cmake-modules-fb4d91996affdbcd3ffc19c7646fbf77b96bf1e5.tar.gz extra-cmake-modules-fb4d91996affdbcd3ffc19c7646fbf77b96bf1e5.tar.bz2 |
Make sure we load translations on the main thread.
Because the old implementation (accidentally) worked when you put the
ecm_create_qm_loader call in a different CMakeLists.txt file to the
target the file was added to, some projects did this.
This won't work with build-time-generated files, though, like moc files.
So we (ab)use QTimer events to make the loading happen on the main
thread.
BUG: 346188
REVIEW: 126000
Diffstat (limited to 'tests/ECMPoQmToolsTest/tr_thread_test.cpp')
-rw-r--r-- | tests/ECMPoQmToolsTest/tr_thread_test.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/ECMPoQmToolsTest/tr_thread_test.cpp b/tests/ECMPoQmToolsTest/tr_thread_test.cpp new file mode 100644 index 00000000..40e4d1eb --- /dev/null +++ b/tests/ECMPoQmToolsTest/tr_thread_test.cpp @@ -0,0 +1,69 @@ +#include <QCoreApplication> +#include <QLibrary> +#include <QMetaObject> +#include <QThread> + +class Thread : public QThread +{ + Q_OBJECT + + QLibrary *m_lib; + +public: + Thread() + : m_lib(0) + {} + ~Thread() + { + delete m_lib; + } + +Q_SIGNALS: + void libraryLoaded(); + +public Q_SLOTS: + void printStrings() + { + Q_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread()); + + QFunctionPointer print_strings = m_lib->resolve("print_strings"); + if (print_strings) { + print_strings(); + } else { + qFatal("Could not resolve print_strings: %s", qPrintable(m_lib->errorString())); + } + + QCoreApplication::instance()->quit(); + } +protected: + void run() + { + m_lib = new QLibrary(MODULE_PATH); + + if (!m_lib->load()) { + qFatal("Could not load module: %s", m_lib->errorString().toUtf8().data()); + } + + // Queue a call to printStrings() on the main event loop (giving the + // translations a chance to be loaded). + QMetaObject::invokeMethod(this, "printStrings", Qt::QueuedConnection); + } +}; + +int main(int argc, char** argv) +{ + QCoreApplication app(argc, argv); + + Thread thread; + + // Start the thread *after* QCoreApplication is started (otherwise the + // plugin's startup function won't be run on the Thread, and we won't test + // what we wanted to test). + QMetaObject::invokeMethod(&thread, "start", Qt::QueuedConnection); + + app.exec(); + + return 0; +} + +#include "tr_thread_test.moc" |