-1

Header:

#ifndef CONTROLLER_THREAD
#define CONTROLLER_THREAD

#include <QThread>

class Worker : public QObject
{
    Q_OBJECT
public:
    Worker();
    ~Worker();

private slots:
    void calculateImage();

signals:
    void imageReady();
};

class Controller: public QObject
{
public:
    Worker  objWorker;
    QThread objQThread;

    Controller();
    ~Controller();

public slots:
    void receiveImage();
};

#endif // CONTROLLER_THREAD

Source:

#include <controller_thread.h>

Worker::Worker(){}
Worker::~Worker(){}

void Worker::calculateImage()
{

}

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

    connect( objWorker, objWorker::imageReady, this, receiveImage );

    objQThread.start();
}

Controller::~Controller(){}

void Controller::receiveImage()
{

}

I am receiving the error:

 error: ‘objWorker’ is not a class, namespace, or enumeration
     connect( objWorker, objWorker::imageReady, this, receiveImage );

                     ^

What is the meaning of this error? How to resolve?

Typing this:
connect( objWorker, &Worker::imageReady, this, &Controller::receiveImage );

results in :

error: no matching function for call to ‘Controller::connect(Worker&, void (Worker::*)(), Controller*, void (Controller::*)())

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

1 Answers1

2

Error is in this line:

connect( objWorker, objWorker::imageReady, this, receiveImage );

In a 'new' Qt5 syntax, you need to provide class name instead of object name here - objWorker::imageReady.

So, your connect statement should be this:

connect( objWorker, &Worker::imageReady, this, &Controller::receiveImage );

For more help with 'new' Qt5 connect syntax you can refer here

Shf
  • 3,463
  • 2
  • 26
  • 42
  • 1
    More generally, that's how we write a **pointer to member function** in C++. – Toby Speight Jun 06 '18 at 10:31
  • yep, detailed explanation is in docs i provided. Did not want complicate things for simple question – Shf Jun 06 '18 at 10:34
  • I typed this: `connect( objWorker, &Worker::imageReady, this, &Controller::receiveImage );`. It resulted in this error: `error: no matching function for call to ‘Controller::connect(Worker&, void (Worker::*)(), Controller*, void (Controller::*)())’ connect( objWorker, &Worker::imageReady, this, &Controller::receiveImage ); ^` – Aquarius_Girl Jun 06 '18 at 10:36
  • 1
    Also noticed, that your `Controller` class does not have `Q_OBJECT` macro in it's definition. Without it signals and slots will not work for that class – Shf Jun 06 '18 at 10:40
  • Thanks, that was one problem. Other problem was that , I need to write connect( &objWorker, rather than connect( objWorker, – Aquarius_Girl Jun 06 '18 at 10:42