Solved Wrong thread with placeholder %{qthreadptr} in own message pattern
-
Hi,
i set an own message pattern withQString pattern = "%{message}. Thread from pattern: %{qthreadptr}" qSetMessagePattern(pattern)
I also used the placeholder %{qthreadptr}. But the thread doesn't change.
For example:
Slot in Thread 1:
void slotInThread1() { qDebug() << "Thread in slot1: " << QThread::currentThread(); // Thread in slot1: QThread(0xd79e70). Thread from pattern: 0xd79e70 }
Slot in Thread 2:
void slotInThread2() { qDebug() << "Thread in slot2: " << QThread::currentThread(); // Thread in slot2: QThread(0x433f788). Thread from pattern: 0xd79e70 }
Can some explain this behavior?
Thanks
-
Hi,
How did you connect these slots ? To what are they connected ?
-
@SGaist
Hi, now i have tested it on my Mac without the problems.#include <QCoreApplication> #include <QThread> #include <QDebug> #include <QTimer> #include <QString> class Worker : public QObject { Q_OBJECT public slots: void doWork(QString param) { qDebug() << param << "thread: " << QThread::currentThread(); } }; #include "main.moc" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString pattern = "%{message}. Thread from pattern: %{qthreadptr}"; qSetMessagePattern(pattern); QThread thread1; QThread thread2; Worker worker1; worker1.moveToThread(&thread1); thread1.start(); Worker worker2; worker2.moveToThread(&thread2); thread2.start(); qDebug() << "Thread in main: " << QThread::currentThread(); QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&worker1, &worker2]() { QMetaObject::invokeMethod(&worker1, "doWork", Q_ARG(QString, "worker1")); QMetaObject::invokeMethod(&worker2, "doWork", Q_ARG(QString, "worker2")); }); timer.start(2000); return a.exec(); }
Output:
Thread in main: QThread(0x100702b10). Thread from pattern: 0x100702b10 "worker1" thread: QThread(0x7fff5fbffad8). Thread from pattern: 0x7fff5fbffad8 "worker2" thread: QThread(0x7fff5fbffac8). Thread from pattern: 0x7fff5fbffac8
So i think i have to search the problem in my code.