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. How can I emit signal from QRunnable or call callback?

How can I emit signal from QRunnable or call callback?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 9.6k Views
  • 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.
  • S Offline
    S Offline
    smartcoder.v.arch
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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();
      }
      
      1 Reply Last reply
      3
      • Q Offline
        Q Offline
        QKelteseth
        wrote on last edited by
        #3

        So just for anyone who still gets errors (like me):
        The order of inheritance is very important. Always include the Object first!

        1 Reply Last reply
        1

        • Login

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