Question

I've encountered a following issue:

I have a GUI element class and a Thread class. I would like to exchange signals between them. However it only works in one direction. I can successfuly send a signal from main GUI window to the thread. However when I want to send a signal from the thread to the process that calls the thread, but it doesn't work. Here is how I do it:

bool GfxMapItem::mapSwitch(int seqidx)
{
    ...
importMapThread_ = new ImportMapThread(sequence_->item(seqidx)); 
connect(GUI_UI->analExportButton,  SIGNAL(clicked()), importMapThread_, SLOT(interrupt1()), Qt::QueuedConnection); //<-- this one works perfectly 
connect(importMapThread_,  SIGNAL(progress(int)), this, SLOT(loadMap(int)), , Qt::QueuedConnection); // <-- this one unfortunatelly doesn't 
    ... 
} 

The thread code is very simple and it only emits the signal (I've check that it works when I connect the thread with itself).

void ImportMapThread::run()
{
QLOGX("Running ImportMapThread..."); 
QLOGINC; 
emit this->progress(100); 

QLOGX("Stopping ImportMapThread..."); 
} 

The thread header looks like the one below:

class ImportMapThread : public InterruptibleThread
{
Q_OBJECT

private: 
...

protected:
...

public:
ImportMapThread(SeqItem *p); 

signals: 
void progress(int value); 
void progressReset(int value); 

public slots: 
     virtual void interrupt1(); 
 void loadMap();  

protected: 
virtual void run(); 
}; 

And the class that calls that thread looks like this:

class GfxMapItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
...

signals:
void mouseOutside();
void mouseMove(const QPointF &pos);

private slots: 
void loadMap(int); 

protected:
...
}; 

Any ideas? Please, I've ran out of ideas.

Was it helpful?

Solution 2

Solved! It turned out that only the GUI elements can communicate with the threads. The thread can be inherited from other type of the thread and there isn't any problem neither with the signals nor the slots. The problem was laying in the object GfxMapItem. For some reason it just couldn't handle normal communication.

OTHER TIPS

I believe, you should call exec in your run-implementation. Exec starts event-loop which should allow to send signals.

Take a look at:

int QThread::exec() [protected] Enters the event loop and waits until exit() is called, returning the value that was passed to exit(). The value returned is 0 if exit() is called via quit(). It is necessary to call this function to start event handling.

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top