Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why does the slot emit it's connected signal ?



  • *Hi !

    I'm using QOpenGL to print a video on a texture for an QML HMI. I'm using a code that I don't understand and that does not work yet. And i'm completely new to OpenGL

    So today's question is :
    Why would a slot emit the signal that calls this same slot ?

    The signal I'm talking about, void newImageAvailable(); is emitted when a new image is ready to be printed. It's emitted periodically by a QThread. i got this. But the slot is only emitting the signal.
    here are the specific samples of code.

    	void ImageSourceShm::onNewImageAvailable(){
    		// Attention : peut-être exécuté dans le thread du monitor
    		Q_EMIT newImageAvailable();
    	}
    

    i'm trying to understand how the code is supposed to print the image get in the shared memory on a texture...



  • @Quentin91
    Are you 100% sure that the newImageAvailable signal is connected to onNewImageAvailable?


  • Moderators

    @quentin91 said in Why does the slot emit it's connected signal ?:

    The signal I'm talking about, void newImageAvailable(); is emitted when a new image is ready to be printed. It's emitted periodically by a QThread.

    Which class contains that signal?

    here are the specific samples of code.

      void ImageSourceShm::onNewImageAvailable(){
      	// Attention : peut-être exécuté dans le thread du monitor
      	Q_EMIT newImageAvailable();
      }
    

    If that signal belongs to ImageSourceShm, then your program will enter an infinite recursion and crash when ImageSourceShm::newImageAvailable() is emitted.



  • @fcarney

    @Quentin91
    Are you 100% sure that the newImageAvailable signal is connected to onNewImageAvailable?

    yes, i am

    connect(m_pMonitorThread, SIGNAL(firstImageAvailable(int, int)), this, SLOT(onFirstImageAvailable(int, int)), Qt::DirectConnection);
    connect(m_pMonitorThread, SIGNAL(newImageAvailable()), this, SLOT(onNewImageAvailable()), Qt::DirectConnection);

    If that signal belongs to ImageSourceShm, then your program will enter an infinite recursion and crash when ImageSourceShm::newImageAvailable() is emitted.

    That's weird as this program is the source code of a pyd lib well spread in my companny... The programs using it don't crash.
    Ok, they are sometimes but mostly works.

    Maybe it's because a thread is emitting the signal periodically too ? i still don't get how this program works to print images...

    	void ImageSourceShmMonitorThreadByPeriod::run()
    	{
    		m_pPrecisionTimer->start();//mesure du temps d'attente
    		while (m_pImageSourceShm->isAttached() == false)
    		{
    			QThread::msleep(1);
    		}m_pData = m_pShm->data();
    		QThread::msleep(1);
    		Q_EMIT firstImageAvailable(m_pImageSourceShm->shm()->getWidth(), m_pImageSourceShm->shm()->getHeight());
    
    		m_pPrecisionTimer->start();
    		while (1)
    		{
    			if (m_pPrecisionTimer->elapsed() >= m_dPeriod)
    			{
    				m_pPrecisionTimer->start();
    				Q_EMIT newImageAvailable();//THERE 
    			}
    			QThread::msleep(1);
    		}
    	}

  • Moderators

    @quentin91 said in Why does the slot emit it's connected signal ?:

    connect(m_pMonitorThread, SIGNAL(newImageAvailable()), this, SLOT(onNewImageAvailable()), Qt::DirectConnection);

    The sender and receiver are different objects. The sequence is:

    1. The signal, m_pMonitorThread->newImageAvailable(), is emitted
    2. The slot, this->onNewImageAvailable(), is invoked by the previous signal
    3. The signal, this->newImageAvailable(), is emitted in the previous slot
    4. Nothing else happens, because nothing is connected to this->newImageAvailable()

    Notice that #1 and #3 are different signals. I can't figure out what the author is trying to do here, however...

    i'm trying to understand how the code is supposed to print the image get in the shared memory on a texture...

    I don't see any image printing code in your posts.

    P.S. FYI, ImageSourceShm subclassses QThread, implements slots in the subclass, implements an infinite while() loop inside run(), and then forces a Qt::DirectConnection. This is an anti-pattern; please don't do this in new code!



  • Thank you very much for this explanation. It's not surprising if I didn't understand then... I'll change this code

    @jksh

    I don't see any image printing code in your posts.

    There aren't any image printing there. For now, a two classes are defined as QQuickItems. One is creating opengl textures, vao and stuff and one other is getting pictures in the sharedMemory. I still don't know how the data are supposed to be sent from the shm classes to the textures classes to be printed but I suppose I'll make another topic about it in a few days...


Log in to reply