Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Wrong thread with placeholder %{qthreadptr} in own message pattern

Wrong thread with placeholder %{qthreadptr} in own message pattern

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 604 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    Hi,
    i set an own message pattern with

    QString 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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How did you connect these slots ? To what are they connected ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      beeckscheB 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How did you connect these slots ? To what are they connected ?

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved