简体   繁体   English

Qt Slot 在通过 std::async 创建的线程上时不会被调用

[英]Qt Slot is not called when it lives on a thread created via std::async

I wanted to create a Qt widget which communicates with other classes on different threads via the signal / slot system.我想创建一个 Qt 小部件,它通过信号/插槽系统与不同线程上的其他类进行通信。 The recieving Objects are created in a Function wich is run via std::async.接收对象是在通过 std::async 运行的函数中创建的。 The problem is: If the widget emits a signal the slot on the other thread is not called.问题是:如果小部件发出信号,则不会调用另一个线程上的插槽。

My Example:我的例子:

I created the Class MainWindow which derives from QMainWindow and will live on the main thread.我创建了从 QMainWindow 派生的类 MainWindow 并将存在于主线程上。 The class Reciever is created in a function which is called via std::async, and has a thread which should print something to the console. Reciever 类是在一个通过 std::async 调用的函数中创建的,并且有一个线程应该向控制台打印一些东西。

I tested if the signal is emitted by connecting it to another slot on the same thread which works fine.我测试了是否通过将信号连接到同一线程上的另一个插槽来发出信号,该插槽工作正常。

MainWindow.hpp主窗口.hpp

#pragma once

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
     explicit MainWindow(QWidget *parent = 0);

signals:
    void send();

private slots:
    void buttonClicked();
    void recieve();
};

MainWindow.cpp主窗口文件

#include "MainWindow.hpp"
#include <iostream>

#include <QPushButton>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QPushButton* start = new QPushButton("Start");
    setCentralWidget(start);
    start->show();

    connect(start, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
    connect(this, SIGNAL(send()), this, SLOT(recieve()));
}

void MainWindow::buttonClicked()
{
    std::cout << "MainWindow::buttonClicked()\n";
    emit send();
}

void MainWindow::recieve()
{
    std::cout << "MainWindow::recieve()\n";
}

Reciever.hpp接收器.hpp

#include <QObject>

class Reciever : public QObject
{
    Q_OBJECT
public:
    Reciever(QObject *parent = 0);

public slots:
    void recieve();
};

Reciever.cpp接收者.cpp

#include "Reciever.hpp"

#include <iostream>

Reciever::Reciever(QObject *parent) : QObject(parent)
{
    std::cout << "Reciever()" << std::endl;
}

void Reciever::recieve()
{
    std::cout << "Reciever::recieve()" << std::endl;
}

main.cpp主程序

#include "MainWindow.hpp"
#include "Reciever.hpp"
#include <QApplication>

#include <future>

void StartAndConnect(MainWindow &widget)
{
    Reciever* rec = new Reciever();

    QObject::connect(&widget, SIGNAL(send()), rec, SLOT(recieve()));
}

int main(int argc, char *argv[])
{

    QApplication app(argc, argv);

    MainWindow myWidget;

    myWidget.show();

    auto future = std::async(std::launch::async, [&myWidget](){
        StartAndConnect(myWidget);
    });

    app.exec();

    future.wait();
}

After some research my strongest guess was, that the thread launched by std::async does not has a Qt event-loop and thus will not come to a point where the posted event (emit) is processed.经过一些研究,我最强烈的猜测是,std::async 启动的线程没有 Qt 事件循环,因此不会到达处理发布事件(emit)的地步。 I changed the main to use QtConcurrent::run but it also did not work.我将主要更改为使用 QtConcurrent::run 但它也不起作用。

EDIT编辑

Here my try with QtConcurrent:在这里我尝试使用 QtConcurrent:

main2.cpp main2.cpp

#include "MainWindow.hpp"
#include "Reciever.hpp"
#include <QApplication>

#include <future>
#include <QtConcurrent>

void StartAndConnect(MainWindow &widget)
{
    Reciever* rec = new Reciever();

    QObject::connect(&widget, SIGNAL(send()), rec, SLOT(recieve()));
}

int main(int argc, char *argv[])
{

    QApplication app(argc, argv);

    MainWindow myWidget;

    myWidget.show();

    auto future = QtConcurrent::run( [&myWidget](){
    StartAndConnect(myWidget);
    });

    app.exec();

    future.waitForFinished();
}

You need a running event loop in your thread, if you want to process cross-thread slot calls.如果要处理跨线程槽调用,则需要在线程中运行事件循环。

auto future = QtConcurrent::run( [&myWidget](){
StartAndConnect(myWidget);
QEventLoop e;
e.exec();
});

But I recomend to use QThread , because in your case it is obvious.但我建议使用QThread ,因为在你的情况下这是显而易见的。 Qt has a very good documentation , that describes your case. Qt 有一个很好的文档,描述了你的情况。

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

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