How can I emit signal from QRunnable or call callback?



  • Hello. I need your help with such a problem. This is an example of a basic code:

    class Runnable : public QRunnable
    {
    	explicit Runnable(QImage* qimg, uint index):m_qimage(qimg),m_index(index)
    	{
            }
    
    	void run()
    	{ 
    		if(m_qimage->scale(1280,720)->save(QString("%1.jpg").arg(index)))
    		{
    			// i need to emit a signal in this place
    		}
    	}
    private:
    	QImage* m_qimage;
    	uint m_index;
    } ;
    
    
    class Controller : public QObject
    {
    	explicit Controller(QObject* parent = 0)
    	{
    		m_pool = new QThreadPool(this);
    	}
    
    	public slots:
    		void slotWithoutName(QImage* qimg,uint index)
    		{
    			Runnable * m_worker = new Runnable(qimg,index);
    			m_pool->start(m_worker);
    		}
    
    		void slotAnsverFromRunnableClass(uint index)
    		{
    			qDebug()<<"Okey , I've done my work!!!\n";
    		}
    	private:
    		QThreadPool* m_pool; 
    };
    

    .....and somewhere in the program :

    connect(this, SIGNAL(frame(QImage* qimg,uint index)),m_controller,SLOT(slotWithoutName(QImage* qimg,uint index)));
    

    Please, tell me how is right to call the implementation of a slot

    			 void slotAnsverFromRunnableClass(uint index)
    

    from class Runnable I tried

    QMetaObject::invokeMethod(m_p_controller,"slotAnsverFromRunnableClass",Qt::AutoConnection,Q_ARG(uint,index));
    

    , but it gave no results and no output to the console QtCreator about the mistake too...
    Tried this one:

    class Runnable : public QObject ,public QRunnable
    {
    	Q_OBJECT
    
    explicit Runnable(QImage* qimg, uint index):m_qimage(qimg),m_index(index)
    {
    
    }
    
    void run()
    { 
    	if(m_qimage->scale(1280,720)->save(QString("%1.jpg").arg(index)))
    	{
    		// i need to emit signal in this place
    		emit mySignal(index);
    	}
    }
    
    signals:
    	
    	void mySignal(uint index);
    	//...
    	 other code
    	//.....
    }; 
    

    but this method gave no results too. As I understand there is any rule of association between streams using signals or calling callback functions which I don't know... Please, help me to solve this problem. Thanks


  • Moderators

    Hi! There is no "special rule"; everything works as expected:

    work.h

    #ifndef WORK_H
    #define WORK_H
    
    #include <QRunnable>
    #include <QDebug>
    #include <QObject>
    
    class Work : public QObject, public QRunnable
    {
        Q_OBJECT
    public:
        void run();
    signals:
        void sayHi();
    };
    
    #endif // WORK_H
    

    work.cpp

    #include "work.h"
    
    #include <QDebug>
    #include <QThread>
    
    void Work::run()
    {
        qDebug() << "Hello from thread " << QThread::currentThread();
        emit sayHi();
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    #include <QThreadPool>
    #include <QDebug>
    #include <QObject>
    #include "work.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        Work work;
        work.setAutoDelete(false);
        QThreadPool *threadPool = QThreadPool::globalInstance();
        threadPool->start(&work);
        QObject::connect(&work, &Work::sayHi, &app, &QApplication::quit);
        return app.exec();
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.