gpt4 book ai didi

c++ - 通过Signal/Slot机制将QString传递给QThread

转载 作者:行者123 更新时间:2023-11-30 01:49:03 24 4
gpt4 key购买 nike

我在让主窗口与工作线程通信时遇到问题。我使用通常的 SIGNAL/SLOT 机制传递 QString 对象。我的程序应该将 QString“Alice”从 MainWindow 传输到在 QThread 中处理的类 Worker 的工作对象,然后再返回。代码编译无误,并在我的 MacBook Pro 上使用 Qt 4.8 和 Clang 顺利运行。这是显示我的问题的最少代码。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <worker.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
QString name;
private slots:
void errorString(QString);

public slots:
void getFromWorker(QString);

signals:
void sendToWorker(QString);
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

name = "Alice";
qDebug() << "The name in MainWindow is: " << name << " from ThreadID = " << QThread::currentThreadId();
ThreadedWork();
qDebug() << "Name sent from the Worker Thread is: " << name;
}

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

void MainWindow::errorString(QString string)
{
qDebug() << "The error from the threaded process is: " << string;
}

void MainWindow::ThreadedWork()
{
Worker* worker = new Worker;
emit sendToWorker(name);
connect(this,SIGNAL(sendToWorker(QString)),worker,SLOT(getFromMain(QString)));
connect(worker,SIGNAL(sendToMain(QString)),this,SLOT(getFromWorker(QString)));

QThread *thread = new QThread;
worker->moveToThread(thread);

connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

thread->start();
}

void MainWindow::getFromWorker(QString someName) //slot implementation
{
name = someName;
}

worker.h

#ifndef WORKER_H
#define WORKER_H

#include <QObject>

class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(Worker * parent = 0);
~Worker();

signals:
void sendToMain(QString);
void error(QString err);
void finished();

public slots:
void getFromMain(QString);
void process();

private:
QString name;
};

#endif // WORKER_H

worker.cpp

#include "worker.h"
#include <QDebug>
#include <QThread>

Worker::Worker(Worker * parent)
{

}

Worker::~Worker()
{

}

void Worker::process() {
// allocate resources using new here
qDebug() << "name recieved from Main is: " << name;
qDebug() << "name passed to Worker Thread is: " << name << " having ThreadID = " << QThread::currentThreadId();
emit sendToMain(name);
emit finished();
}

void Worker::getFromMain(QString someName) // slot implementation
{
name = someName;
}

我的输出是:

The name in MainWindow is:  "Alice"  from ThreadID =  0x7fff7de7a300 
Name sent from the Worker Thread is: "Alice"
name recieved from Main is: ""
name passed to Worker Thread is: "" having ThreadID = 0x1107a9000

如上所示,我无法将 QString“Alice”传递给工作人员。我在这里缺少什么?

最佳答案

ThreadedWork() 方法的问题:

您首先发出信号 emit sendToWorker(name); 并且只有它们将此信号连接到插槽 - 这是您失败的根源。

像这样改变行:

void MainWindow::ThreadedWork()
{
Worker* worker = new Worker;

connect(this,SIGNAL(sendToWorker(QString)),worker,SLOT(getFromMain(QString)));
connect(worker,SIGNAL(sendToMain(QString)),this,SLOT(getFromWorker(QString)));

emit sendToWorker(name);

QThread *thread = new QThread;
worker->moveToThread(thread);
...
}

我大概得到了预期的结果:

The name in MainWindow is:  "Alice"  from ThreadID =  0xcbc
Name sent from the Worker Thread is: "Alice"
name recieved from Main is: "Alice"
name passed to Worker Thread is: "Alice" having ThreadID = 0xff8

关于c++ - 通过Signal/Slot机制将QString传递给QThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com