Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在线程中使用QTimer生成类_Qt_Qthread_Qtimer - Fatal编程技术网

在线程中使用QTimer生成类

在线程中使用QTimer生成类,qt,qthread,qtimer,Qt,Qthread,Qtimer,我需要一个类,通过计时器从另一个线程发送数据 像这样: QPointer<Checker> checker; connect(checker, &Checker::newData, this, &MyClass::process, Qt::BlockingQueuedConnection); // process(QMap<QString, int>) 这个代码正确吗? 有没有更简单的方法? 为什么QueuedConnection不连接?(Blockin

我需要一个类,通过计时器从另一个线程发送数据 像这样:

QPointer<Checker> checker;
connect(checker, &Checker::newData, this, &MyClass::process, Qt::BlockingQueuedConnection); // process(QMap<QString, int>)
这个代码正确吗? 有没有更简单的方法?
为什么QueuedConnection不连接?(BlockingQueuedConnection-已工作)

您应该首先连接,然后启动线程。这样就不会发出信号,也不会启动计时器!此外,根据我的经验,您实际上不需要设置连接类型(排队/阻塞),因为Qt在这方面做得很好,谢谢。我修复了它,但代码仍在运行。为什么使用QueuedConnection成功连接到MyClass,但不起作用?使用Qt::BlockingQueuedConnection-work。您可能会在运行时收到一条消息,提示
QObject::connect:Cannot queue arguments of type
。您需要注册该类型才能使排队连接工作。谢谢,我收到了这个错误。
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>);
};
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();
}