#include <QTimer>
#include <QMutex> //多線程
#include <QThread>
QThread* plantimer_thread = new QThread(this);
QTimer* plantimer = new QTimer();
plantimer->moveToThread(plantimer_thread);
plantimer->setInterval(1500);
//connect(plantimer_thread,&QThread::start, plantimer,&QTimer::start);//通過線程開啟來觸發定時器開啟
connect(plantimer_thread, SIGNAL(started()), plantimer, SLOT(start()));//通過線程開啟來觸發定時器開啟
connect(plantimer, &QTimer::timeout, this, &MainWindow::timerAutoSendSlots, Qt::DirectConnection);//信號發出后立即調用槽函數,槽函數在發出信號的線程中執行
connect(plantimer_thread, &QThread::finished, this, &QObject::deleteLater);//線程結束,自動關閉線程
plantimer_thread->start();
void MainWindow::timerAutoSendSlots()
{
qDebug() << QStringLiteral("當前線程id:") << QThread::currentThread();
}