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. [Solved]Can't receive signal from another thread
Forum Updated to NodeBB v4.3 + New Features

[Solved]Can't receive signal from another thread

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.1k Views 1 Watching
  • 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.
  • U Offline
    U Offline
    useryy
    wrote on last edited by
    #1

    Can't receive signal from another thread as following.
    Using connect type as Qt::BlockingQueuedConnection, the thread blocks. But signal not received.

    main.cpp :

    @#include "myclass.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    MyClass myClass;
    myClass.startAll();
    return a.exec();
    }
    @

    thread.h :

    @
    #include <QtCore/QCoreApplication>
    #include <QThread>
    #include <QDebug>

    class Thread1 : public QThread
    {
    Q_OBJECT
    public:
    void run( void )
    {
    qDebug() << "thread 1 started";
    int i = 0;
    while(1)
    {
    msleep( 10 );
    i++;
    qDebug() << "i is: " + QString::number(i);
    if(i==1000)
    {
    qDebug() << "Got I";
    emit MyThread1Signal();
    exit();
    }
    }
    }
    signals:
    void MyThread1Signal();
    };@

    myclass.cpp :

    @
    MyClass::MyClass(QObject *parent) :
    QObject(parent)
    {
    thread1 = new Thread1();
    connect(thread1, SIGNAL(MyThread1Signal()), this, SLOT(myClassSlot1()), Qt::BlockingQueuedConnection);
    }

    void MyClass::startAll()
    {
    thread1->start();
    thread1->wait();
    }

    void MyClass::myClassSlot1()
    {
    qDebug()<<"Signal received";
    }@

    myclass.h :

    @
    #ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>

    #include "threads.h"

    class MyClass : public QObject
    {
    Q_OBJECT
    public:
    explicit MyClass(QObject *parent = 0);
    void startAll();

    signals:

    public slots:
    void myClassSlot1();

    private:
    Thread1* thread1;
    };

    #endif // MYCLASS_H
    @

    [edit: please add @ code tags. Did it for you, Eddy]

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      Things I will try:

      1. move the connect from the constructor to the startAll method, between the start and wait of the thread
      2. change the exit with a simple return
      3. leave the connection in automatic mode (remove blocked queue).

      But I'm totally not sure about any of them.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        useryy
        wrote on last edited by
        #3

        Change exit to return works. (Or remove wait() after start())

        For these prevent thread from sleep or terminate.

        Thanks.

        1 Reply Last reply
        0

        • Login

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