我在 Qt 线程方面遇到一些问题,以允许线程部分更新我程序的 GUI。这似乎是 Qt 的一个已知“问题”,所以我找到了多个教程,但我不明白为什么我的示例在这里不起作用。
我继承自QThread如下:
class CaptureThread: public QThread {
Q_OBJECT
public:
CaptureThread(const QObject *handler, QPushButton *start) {
CaptureThread::connect(start, SIGNAL(clicked()), this, SLOT(capture_loop()));
CaptureThread::connect(this, SIGNAL(sendPacket(Packet*)),
this, SLOT(receivePacket(Packet*)));
}
signals:
void sendPacket(Packet*);
public slots:
void capture_loop() {
Packet *packet;
while (my_condition) {
packet = Somewhere::getPacket();
//getPacket is define somewhere and is working fine
emit(sendPacket(packet));
std::cout << "Sending packet!" << std::endl;
}
}
};
这是 CaptureHandler:
class CaptureHandler: public QWidget {
Q_OBJECT
public:
CaptureHandler() {
start = new QPushButton("Capture", this);
thread = new CaptureThread(this, start);
thread->start();
}
public slots:
void receivePacket(Packet *packet) {
std::cout << "Packet received!" << std::endl;
/*
Here playing with layout etc...
*/
}
private:
QPushButton *start;
CaptureThread *thread;
};
我认为信号和槽没问题,因为它显示在终端上
Sending packet!
Packet received!
Sending packet!
Packet received!
Sending packet!
Packet received!
但是在 receivePacket 插槽中,我试图修改我的 GUI,但它不起作用。 GUI 只是卡住,我所能做的就是在终端上按 CTRL+C。 所以我认为我的 capture_loop,目前是一个无限循环,正在阻塞程序,这意味着我的线程还没有启动。
但是我调用了thread->start()。 我什至尝试通过调用 this->start() 在 CaptureThread 构造函数中启动线程,但结果是一样的。
有什么想法吗?
最佳答案
您使用的QThread
错误。通过只创建线程,它不会在线程上执行。您将需要在 QThread::run
函数中执行此操作(通过覆盖它),因为这是唯一一个将在新线程上运行的函数。请注意,一旦您从此函数返回,线程就会退出。
如果您想在 QThread::run
函数中使用自己的循环(而不是使用 Qts 默认事件循环),线程将无法在运行函数中接收信号!
这里有一个关于如何使用QThread
的例子:
class CaptureThread: public QThread {
Q_OBJECT
public:
CaptureThread(const QObject *handler, QPushButton *start) {
//calling "start" will automatically run the `run` function on the new thread
CaptureThread::connect(start, SIGNAL(clicked()), this, SLOT(start()));
//use queued connection, this way the slot will be executed on the handlers thread
CaptureThread::connect(this, SIGNAL(sendPacket(Packet*)),
handler, SLOT(receivePacket(Packet*)), Qt::QueuedConnection);
}
signals:
void sendPacket(Packet*);
protected:
void run() {
Packet *packet;
while (my_condition) {
packet = Somewhere::getPacket();
//getPacket is define somewhere and is working fine
emit sendPacket(packet) ;//emit is not a function
qDebug() << "Sending packet!";//you can use qDebug
}
}
};
关于c++ - QThread with slots and signals 似乎没有创建新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33845957/