问题说明
fetchParam
QThread(0x1d23120) -index: 0
showAudioInfo
QThread(0x1d231c0) -index: 1
showAudioInfo
但现在打印:
fetchParam
QThread(0x1d23120) -index: 0
QThread(0x1d231c0) -index: 0
showAudioInfo
showAudioInfo
how to deal?
程序:
//MyWidget class
int MyWidget::index = 0;
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
ui->le_ThreadNum->setText("2");
//create thread
createThread(THREADNUM);
connect(ui->pb_test, &QPushButton::clicked, this, &MyWidget::dealSignal);
}
void MyWidget::dealSignal()
{
int num = ui->le_ThreadNum->text().toInt();
int count = 0;
if (num>THREADNUM)
{
count = num - THREADNUM;
createThread(count);
}
//connect signal and slot
connect(this, &MyWidget::toWork, distinguishFileTaskList.at(0), &MyTask::dealToWork);
connect(distinguishFileTaskList.at(0), &MyTask::toFetchParam, this, &MyWidget::fetchParam);
for (int i = 0; i<num; i++)
{
connect(this, &MyWidget::toDistinguish, distinguishFileTaskList.at(i), &MyTask::dealDistinguishFile);
connect(distinguishFileTaskList.at(i), &MyTask::toShowAudioInfo, this, &MyWidget::showAudioInfo);
}
}
void MyWidget::fetchParam()
{
qDebug() << "fetchParam";
emit toDistinguish(index);
}
void MyWidget::showAudioInfo()
{
qDebug() << "showAudioInfo";
index++;
}
//MyTask class (thread invoke code)
void MyTask::dealDistinguishFile(int index)
{
qDebug() << QThread::currentThread() << "-index:" << index;
emit toShowAudioInfo(index);//send signal
}
void MyTask::dealToWork()
{
emit toFetchParam();
}
答案 0 :(得分:0)
通常,您可以在发送方线程中定义自定义Qt信号,并将其连接到线程外的某个插槽。默认情况下,线程间通信使用Qt信号和插槽(排队信号)进行线程安全。