QT 开启新进程注意事项

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)
    }

给覆盖了 故无法实现通讯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章