connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(slotStart()));//信號關聯
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(slotStop()));
for(int i=0;i<MAXSIZE;i++)
{
workThread[i]=new WorkThread(); //(1)
}
//workThread[0]->start();
//thread = new WorkThread();
connect(workThread[0], SIGNAL(returnResult(int)), this, SLOT(displayResult(int)), Qt::QueuedConnection);
connect(workThread[1], SIGNAL(returnResult(int)), this, SLOT(displayResult(int)), Qt::QueuedConnection);
//信號和槽 但有信號時 就會啓動槽 我只要一個connect 當程序結束的時候 讓sum變化
//connect(area,SIGNAL(valueChanged(int)), this,SLOT(spinChange(int)));
//connect(thread, SIGNAL(returnResult2(int)), this, SLOT(displayResult2(int)));
}
void MainWindow::slotStart()
{
// for(int i=0;i<MAXSIZE;i++)
// {
// workThread[i]=new WorkThread(); //(a)
// }
for(int i=0;i<MAXSIZE;i++)
{
workThread[i]->start(); //(b)
}
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(true);
}
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(slotStart()));//信號關聯
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(slotStop()));
將開始與暫停按鈕與開始暫停線程程序相連接
connect(workThread[0], SIGNAL(returnResult(int)), this, SLOT(displayResult(int)), Qt::QueuedConnection);
connect(workThread[1], SIGNAL(returnResult(int)), this, SLOT(displayResult(int)), Qt::QueuedConnection);
這兩行是信號槽的連接 我希望通過按開始按鈕來運行run函數
emit returnResult(6);//run中含有該函數 可以用來實現通信
connect(workThread[0], SIGNAL(returnResult(int)), this, SLOT(displayResult(int)), Qt::QueuedConnection);
我希望是能打印出6 但是由於
// for(int i=0;i<MAXSIZE;i++)
// {
// workThread[i]=new WorkThread(); //(a)
// }
這段函數將前面的
for(int i=0;i<MAXSIZE;i++)
{
workThread[i]=new WorkThread(); //(1)
}
給覆蓋了 故無法實現通訊