简体   繁体   English

如何将信号从线程连接到插槽?

[英]How to connect a signal from a thread to a slot?

What I simply want to do is connect a signal inside a thread to a slot in the main thread to handle UI changes. 我只想做的就是将线程内部的信号连接到主线程中的插槽以处理UI更改。

This is basically the current state of my thread, nothing fancy but it's just for testing purposes atm: 这基本上是我线程的当前状态,没什么花哨的,但仅用于测试atm:

// synchronizer.h
class Synchronizer : public QObject
{
    Q_OBJECT

public:
    Synchronizer();

signals:
    void newConnection(std::wstring id);

private:
    QTimer timer;

private slots:
    void synchronize();
}

// synchronizer.cpp
Synchronizer::Synchronizer()
{
    connect(&timer, SIGNAL(timeout()), this, SLOT(synchronize()));
    timer.start();
}

void Synchronizer::synchronize()
{
    emit newConnection(L"test");
}

And here's how my MainWindow looks: 这是我的MainWindow的外观:

// mainwindow.h
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    Synchronizer synchronizer;

private slots:
    void addConnection(std::wstring id);
}

// mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&synchronizer, SIGNAL(newConnection(std::wstring)),
            this, SLOT(addConnection(std::wstring)));
    QThread *thread = new QThread;
    // The problems starts here?
    synchronizer.moveToThread(thread);
    thread->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::addConnection(std::wstring id)
{
    // Add a new connection to QListWidget
    ui->connectionList(QString::fromStdWString(id));
}

If I remove there lines: 如果我删除那几行:

synchronizer.moveToThread(thread);
thread->start();

everything seems to work as expected, that is a new item is added every second to a QListWidget but as soon as I move the synchronizer object to thread it simply stops working. 一切似乎都按预期工作,即每秒将一个新项添加到QListWidget,但是一旦我将同步器对象移动到线程中,它就会停止工作。 I'd presume it has something to do with the connect context but I'm not really sure how something like this should be achieved as I'm quite new to Qt. 我以为它与连接上下文有关,但是我不确定如何实现这样的事情,因为我对Qt还是很新的。

It seems that the in this case was simply the fact that I am using std::wstring as an argument in the signal without registering the type first and after adding the following line qRegisterMetaType<std::wstring>("std::wstring"); 似乎在这种情况下,这仅仅是我在没有先注册类型而是在添加以下行qRegisterMetaType<std::wstring>("std::wstring");情况下,先在信号中使用std :: wstring作为参数的事实。 qRegisterMetaType<std::wstring>("std::wstring"); to the code, everything worked as expected. 到代码,一切都按预期工作。

If I would have read the output console more carefully I would have solved the problem without too much hassle as it was clearly stated that: 如果我会更仔细地阅读输出控制台,那么我会很轻松地解决此问题,因为它明确指出:
QObject::connect: Cannot queue arguments of type 'std::wstring'

So simply speaking, read the compiler output and don't be stupid like me :) 简单来说,阅读编译器输出,不要像我一样愚蠢:)

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

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