Qt Object::connect: No such slot Signal to Thread Slot - c++

i try to invoke Slot in thread object when threas started but getting this error:
Object::connect: No such slot Worker::doWork(pFoo)
the thread executing code:
// main class
m_WorkerThread = new QThread();
FooStack* pfooStack = InternalStorageManager::getInstance()->getStack();
m_Worker = new Worker();
bool done = connect(m_WorkerThread,
SIGNAL(started()),
m_Worker,
SLOT(doWork(pfooStack)));
m_Worker->moveToThread(m_WorkerThread);
m_WorkerThread->start();
// class Worker
// cpp imple
void Worker::doWork(FooStack *& rp_urlsStack)
{
}
// header
class Worker : public QObject
{
Q_OBJECT
public :
Worker();
~Worker();
public slots:
void doWork(FooStack *&);
};

Object::connect: No such slot Worker::doWork(pFoo)
You can't pass objects in connection declarations.
Can't you pass pfooStack into the Worker constructor?
EDIT:
class Main : ...
{
...
void startThread(); // The method in your example.
private slots:
void startWork();
...
};
void Main::startThread()
{
m_WorkerThread = new QThread();
m_Worker = new Worker();
bool done = connect(m_WorkerThread, SIGNAL(started()),
this, SLOT(startWork()));
m_Worker->moveToThread(m_WorkerThread);
m_WorkerThread->start();
}
void Main::startWork()
{
m_Worker->doWork(InternalStorageManager::getInstance()->getStack());
}

I have not compiled the code on my computer, but it should imply what you need:
m_WorkerThread = new QThread();
FooStack* pfooStack = InternalStorageManager::getInstance()->getStack();
m_Worker = new Worker(pfooStack);
bool done = connect(m_WorkerThread,
SIGNAL(started()),
m_Worker,
SLOT(doWork()));
m_Worker->moveToThread(m_WorkerThread);
m_WorkerThread->start();
void Worker::doWork()
{
//use stack here
}
class Worker : public QObject
{
Q_OBJECT
public :
Worker(FooStack *& rp_urlsStack):stack(rp_urlsStack);
~Worker();
public slots:
void doWork();
private:
FooStack*& stack;
};

You can't do it that way, you can't pass current variables as slot method parameters in connect, and slot can't have more parameters than the signal. In addition to other answers, you can achieve this with QSignalMapper, but if you have just one connection to the slot, that seems like an overkill.
If you can use Qt5 and C++11, then you can connect signal to lambda functions, not just slots, but I'm not absolutely sure if that supports creating a closure (that is, using the local variable in the lambda function, which you would need here).

I think you need to change the signal and slot signatures. From the QT Documenation:
The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.

Related

Should I call processEvents() on a thread?

QThread documentation suggests two ways to make code run in a separate thread. If I use moveToThread approach, I have to call processEvents() to issue the timeouts, to have the lambda executed. And this seems to cost a lot of CPU. Why is so?
class Worker : public QObject
{
Q_OBJECT
QTimer* timer;
bool m_abort = false;
public:
Worker() {}
void abort() {m_abort = true;}
public slots:
void run() {
timer = new QTimer;
connect(timer, &QTimer::timeout, []{qDebug() << "computed";});
timer->start(1000);
forever {
if (m_abort) break;
QCoreApplication::processEvents();
}
}
};
class MainWidget : public QWidget
{
Q_OBJECT
QThread thread;
Worker* worker;
public:
MainWidget()
{
worker = new Worker;
worker->moveToThread(&thread);
connect(this, &MainWidget::start, worker, &Worker::run);
thread.start();
emit start();
}
~MainWidget(){worker->abort(); thread.quit(); thread.wait();}
signals:
void start();
};
However if I subclass QThread and reimplement run() it's not necessary to call processEvents. And CPU cost seems lower. Why?
class Worker : public QThread
{
public:
Worker() {}
protected:
void run() override {
QTimer timer;
connect(&timer, &QTimer::timeout, []{qDebug() << "computed";});
timer.start(1000);
exec();
}
};
class MainWidget : public QWidget
{
Q_OBJECT
Worker* worker;
public:
MainWidget()
{
worker = new Worker;
worker->start();
}
};
your run() function 'blocks' the thread. It is being invoked in the thread context, but never returns. This means, the event loop in the thread doesn't get executed anymore as soon as your run() funtion is called.
For the timer events to call your lambdas, the event loop has to be processed.
If you would modify your run function like this:
void run() {
timer = new QTimer(this);
connect(timer, &QTimer::timeout, []{qDebug() << "computed";});
timer->start(1000);
// don't loop here, instead exit the function
// and let the thread return back to the event loop
}
then your lambdas should get called. The thread will also keep running until you call thread.quit()
note: you can also connect directly to the '''started''' signal of the thread:
connect(&thread, &QThread::started, worker, &Worker::run);
thread.start();
moveToThread approach might be improved by calling run() function just after thread emit started.
But I still don't know why the way I put it initially doesn't work.

Signal is not calling slot from another thread

I am trying to update gui from another thread by using this: https://stackoverflow.com/a/14546167/2236297
This is my workerthread:
class WorkerThread : public QThread {
void run() {
while(1) {
//QTcpSocket messenger;
//messenger.connectToHost("192.168.1.20", 61000);
//if(!messenger.waitForConnected(3000))
//{
emit progressChanged("111");
//}
}
}
// Define signal:
signals:
void progressChanged(QString q)
{
}
};
Only difference is I declared this class as a inner class and defined progressChanged. In his example signal was just declared, not defined.
My onProgressChanged:
void ApplicationController::onProgressChanged(QString info)
{
// Processing code
ui.label_2->setText("Latest info: " + info);
}
This is how I start thread in my constructor:
ApplicationController::ApplicationController(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
WorkerThread *workerThread = new WorkerThread;
// Connect our signal and slot
connect(workerThread, SIGNAL(progressChanged(QString)),
SLOT(onProgressChanged(QString)));
// Setup callback for cleanup when it finishes
connect(workerThread, SIGNAL(finished()),
workerThread, SLOT(deleteLater()));
// Run, Forest, run!
workerThread->start(); // This invokes WorkerThread::run in a new thread
}
Question is: why my slot isn't called. What should I change?
EDIT
Changed workerthread.
.h file
class WorkerThread : public QThread
{
Q_OBJECT
public:
WorkerThread();
~WorkerThread();
void run();
private:
signals:
void progressChanged(QString info);
};
.cpp file
WorkerThread::WorkerThread() { }
WorkerThread::~WorkerThread() { }
void WorkerThread::run()
{
while(1) {
QTcpSocket messenger;
messenger.connectToHost("192.168.1.20", 61000);
if(!messenger.waitForConnected(3000))
{
emit progressChanged((QString("%1").arg(messenger.error())));
}
}
}
In debug, emit works. But slot is not called in main function.
SOLVED
There are multiple errors:
First of all workerThread should be defined as seperate class not an inner class.
Slot should be declared under slots:
public slots:
void onProgressChanged(QString info);

Qt signal slot with threads

I have a problem with signal/slots in a QThread class. My design looks like this:
class Manager : public QObject {
Q_OBJECT
public:
Manager(QObject* parent) : QObject(parent) {
Thread thread* = new Thread(this);
connect(this, SIGNAL(testsignal()), thread, SLOT(test()));
thread->start();
...
emit testsignal();
}
signals:
void testsignal();
};
class Thread : public QThread {
Q_OBJECT
public slots:
void test() {
qDebug() << "TEST";
}
private:
void run() {}
};
The signal never reaches my test() method. Can someone help? Thanks.
The problem is that sending signals across threads results in queuing the signal into the target thread's event queue (a queued connection). If that thread never processes events, it'll never get the signal.
Also, according to the QThread::run documentation:
Returning from this method will end the execution of the thread.
In other words, having an empty run method results in instant termination of the thread, so you're sending a signal to a dead thread.
Signals sent to a QThread object will go to the thread of the parent object. In this case to the same thread that created it.
To have a object live on another thread you should move it to that thread:
class Manager : public QObject {
Q_OBJECT
public:
Manager(QObject* parent) : QObject(parent) {
Thread thread* = new QThread(this);
Receiver* rec = new Receiver(); //no parent
connect(this, SIGNAL(testsignal()), rec, SLOT(test()));
connect(thread, SIGNAL(finished()), rec, SLOT(deleteLater()));
rec->moveToThread(thread);
thread->start();
...
emit testsignal();
}
signals:
void testsignal();
};
class Receiver: public QObject {
Q_OBJECT
public slots:
void test() {
qDebug() << "TEST";
}
};

create qt thread event loop

I am using Qt in order to write a GUI application.
A main thread is responsible for the GUI and creates an QThread in order to do some work with an object.
class Worker
{
void start() {
QTimer* timer = new Timer();
connect(timer,SIGNAL(timeout()),this,SLOT(do()));
}
void do() {
//do some stuff
emit finished();
}
}
class GUI
{
//do some GUI work then call startWorker();
void startWorker() {
QThread* thread = new Thread();
Worker* worker = new Worker();
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(start()));
connect(worker, SIGNAL(finished()), workerthread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
}
}
Now I have several problems:
The timer in my worker class does not work. Maybe it is because the new thread has no event loop, but I have no idea how to create such one. I tried
connect(workerthread, SIGNAL(started()), workerthread, SLOT(exec()));
but it does not work either.
When I try to wait on the new thread, the signal is never sent
class GUI
{
void exit() {
thread->wait();
}
}
I think it also is because there is no event loop and because of that no signal is emitted.
Does anybody have an idea how to solve these problems?
why not use qthreadpool, than you make your task class inherits from qrunnable and qobject, this way you can use signals and slots to pass data from one thread to another, is much simpler to implement, and increase performance from not recreating a thread or having one sleeping all the time
class myTask : public QObject, public QRunnable{
Q_OBJECT
protected:
void run(); //where you actually implement what is supposed to do
signals:
void done(int data);//change int to whatever data type you need
}
//moc click example, or use a timer to call this function every x amount of time
void button_click(){
myTask *task = new myTask();
task->setAutoDelete(true);
connect(task,SIGNAL(done(int)),this,SLOT(after_done(int)),Qt::QueuedConnection);
QThreadPool::globalInstance()->start(task);
}
by default you application gets 1 thread automatically, which you can use to handle the graphic, than use the qthreadpool to process the data/object on demand, you can even set the max amount of threads your application can use to process new request, the others will stay in a queue until one thread is freed
QThreadPool::globalInstance()->setMaxThreadCount(5);
this is a sample code for you :
QThread* thread = new QThread();
Worker* worker = new Worker(3000);
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(start()));
thread->start();`
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(qint32,QObject *parent = 0);
qint32 myTime;
signals:
void workFinished();
public slots:
void doWork();
void start();
private:
QTimer *timer;
};
#include "worker.h"
#include <QTimer>
#include <QDebug>
Worker::Worker(qint32 t,QObject *parent) :
QObject(parent)
{
myTime=t;
}
void Worker::start()
{
timer = new QTimer();
timer->start(myTime);
qDebug()<<QString("start work in time:%1").arg(myTime);
connect(timer,SIGNAL(timeout()),this,SLOT(doWork()));
}
void Worker::doWork()
{
qDebug()<<"dowork";
timer->stop();
emit workFinished();
}
Debug results :
start work in time:3000
I hope this helps you.

Qt Threads: Signals/Slots 2 Objects (1 Singleton) created in Main Thread and called in another Thread

I have a Problem:
I've got 2 objects wich are both created and connected in the main thread. One of the objects is a Singleton.
Now I want to call a method from the singleton in another thread and emit a signal in it.
The Problem is, that the connected slot is not called after the signal was emitted. Am I doing something wrong or is connect() not capable of it?
The Second Thread should process some folders and call a method from the singleton object, which is emitting the signal, with the result of the process.
The Code looks something like this (not working):
void main() {
Singleton* singleton = Singleton::getInstance();
ShowResult* show = new Showesult();
connect(singleton, SIGNAL(itemsset(Items)), show, SLOT(showresult(Items)));
Process* p = new Process();
QThread thread;
p->moveToThread(thread);
connect(&thread, SIGNAL(started()), p, SLOT(process()));
thread.start();
}
class Process : public QObject {
public:
Process(){}
public slots:
void process() {
// Do some work
Singleton::getInstance()->setItems(someitems);
}
};
class Singleton : public QObject {
public:
Singleton(){}
Singleton* getInstance() {
return instance;
}
void setitems(Items) {
//Set items
emit itemsset();
}
signals:
void itemsset(Items);
};
class ShowResult : public QObject {
public:
ShowResult(){}
public slots:
void showresult(Items) {
//THIS Slot is not called...
}
}
Thanks for your help...