是这个意思吗?
#ifndef TASK_H
#define TASK_H
#include <QThread>
#include <iostream>
using namespace std;
class Worker : public QObject
{
Q_OBJECT
public slots:
void slot1() {
emit message(QString("SLOT1 runs in 0x%1").arg((int)thread()->currentThreadId(), 0, 16));
}
void slot2() {
emit message(QString("SLOT2 runs in 0x%1").arg((int)thread()->currentThreadId(), 0, 16));
}
signals:
void message(const QString &result);
};
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
public:
Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate1, worker, &Worker::slot1);
connect(this, &Controller::operate2, worker, &Worker::slot2);
connect(worker, &Worker::message, this, &Controller::handleResults);
workerThread.start();
}
~Controller() {
workerThread.quit();
workerThread.wait();
}
public slots:
void handleResults(const QString &str)
{
cout << str.toStdString() << endl;
}
signals:
void operate1();
void operate2();
};
#endif // TASK_H
#include <QCoreApplication>
#include <iostream>
#include "task.h"
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << "Main Thread ID: " << qApp->thread()->currentThreadId() << endl;
Controller ctl;
emit ctl.operate1();
emit ctl.operate2();
return a.exec();
}
【 在 zhanghaoX 的大作中提到: 】
: 模型1的worker的两个slot 怎么解?
--
FROM 123.123.134.*