简体   繁体   English

QT5中的信号和插槽

[英]SIGNALS and SLOTS in QT5

I've encountered a following issue: 我遇到了以下问题:

I have a GUI element class and a Thread class. 我有一个GUI元素类和一个Thread类。 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. 我可以从GUI主窗口向线程成功发送信号。 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. 拜托,我的想法已经用光了。

I believe, you should call exec in your run-implementation. 我相信,您应该在运行实现中调用exec。 Exec starts event-loop which should allow to send signals. Exec启动事件循环,该循环应允许发送信号。

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(). int QThread :: exec()[保护]进入事件循环并等待直到调用exit(),然后返回传递给exit()的值。 The value returned is 0 if exit() is called via quit(). 如果通过quit()调用exit(),则返回的值为0。 It is necessary to call this function to start event handling. 必须调用此函数来开始事件处理。

I hope this helps. 我希望这有帮助。

Solved! 解决了! It turned out that only the GUI elements can communicate with the threads. 事实证明,只有GUI元素可以与线程通信。 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. 问题出在对象GfxMapItem中。 For some reason it just couldn't handle normal communication. 由于某种原因,它无法处理正常的通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM