我需要一个通过计时器从另一个线程发送数据的类 像这样:
QPointer<Checker> checker;
connect(checker, &Checker::newData, this, &MyClass::process, Qt::BlockingQueuedConnection); // process(QMap<QString, int>)
我的认识:.h
class Checker: public QObject
{
Q_OBJECT
QThread m_thread;
QTimer m_timer;
signals:
void stop();
private slots:
void started() { m_timer.start(1000); }
void stoped() { m_timer.stop(); }
void timeout();
public:
Checker();
~Checker();
Q_SIGNAL void newData(QMap<QString, int>);
};
.cpp
void Checker::timeout()
{
emit newData({});
}
Checker::Checker()
{
this->moveToThread(&m_thread);
m_timer.moveToThread(&m_thread);
m_thread.start();
connect(&m_thread, &QThread::started, this, &Checker::started);
connect(this, &Checker::stop, this, &Checker::stoped);
connect(&m_timer, &QTimer::timeout, this, &Checker::timeout);
}
Checker::~Checker()
{
emit stop();
m_thread.quit();
m_thread.wait();
}
此代码正确吗? 有更容易的方法吗? 为什么QueuedConnection无法连接? (BlockingQueuedConnection-起作用)