-1

Two.h

#ifndef TWO_H
#define TWO_H

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>

class Two : public QObject
{
   Q_OBJECT

private:
    QTimer abc;
public:
    QString m_xyz;
    Two();

signals:
    void emitThisSignal( int x, QString &y );

public slots:
    void mySlot();
};


class Controller : public QObject
{
    Q_OBJECT

private:
    Two objTwo;

    QThread objQThread;

    Controller();

public slots:
    void mySlot( int x, QString &y)
    {
        qDebug() << "\nWWWWWWWWWWWWW: " << y;
    }
};

#endif // TWO_H

Two.cpp

#include "two.h"

Two::Two()
{
    m_xyz = "aksja";

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Two::mySlot);
    timer->start(1000);
}

void Two::mySlot()
{
    emit emitThisSignal(4, m_xyz);
    qDebug()<< "FFFFFFFFFFF " << m_xyz;
}

Controller::Controller()
{
    objTwo.moveToThread( &objQThread );

    connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );
    connect( &objQThread, &QThread::finished, &objQThread, &QThread::deleteLater );

    objQThread.start();
}

Controller::~Controller()
{
    delete objTwo;
    objQThread.wait();
}

I can see that the signal is being emitted because of the print statement but the slot of the Controller class is not getting called.

void Two::mySlot()
    {
        emit emitThisSignal(4, m_xyz);
        qDebug()<< "FFFFFFFFFFF " << m_xyz;
    }

Why is that so?

int main( int argc, char* argv[])
{
    QCoreApplication app(argc, argv);
    Controller o;

    return app.exec();
}
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

1 Answers1

4

See documentation of QObject::connect, note last argument with default value: Qt::AutoConnection.

Its documentation says:

(Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.

Now you are fall in into Qt::QueuedConnection scenario:

The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

So basically you need something which will provide an event loop.

In this code you need that:

int main( int argc, char* argv[])
{
    QCoreApplication app{argc, argv};
    Controller o;

    // note you need something what will stop this event loop to terminate application
    return app.exec();
}

One more thing. Now I noticed that your signals and slot argument is quite unusual. Problem might be second argument which type is QString&.

It might be source of problems I do not know if Qt is able to marshal non const references. If you will add const then it will be able to marshal QString and should work (if I didn't missed other pitfall).

Waqar
  • 8,558
  • 4
  • 35
  • 43
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • it should. Ensure you rebuild it correctly. I do not have time now to verify this on my machine. Maybe in 10 hours – Marek R Jul 31 '20 at 08:57
  • @Aquarius_Girl One question per post please. As to your edit, I already answered this in your question earlier but seems like you didn't bother to read it. You are deleting an object(`objTwo`) on the stack. That's undefined behaviour. And `deleteLater` is completely pointless here. – Waqar Jul 31 '20 at 09:55
  • @Waqar I did read your post and did understand it. I just didn't bother to delete the deleteLater statement because it is not currently creating any trouble. – Aquarius_Girl Jul 31 '20 at 10:33