C++ (Qt)QTime time;time.start();while (1) { //some if (time.esapsed () > ???) { emit newDataAvailable(); time.start(); }}
dbthread = new QThread; impthread = new QThread; worker = new DbWriteWorker(); worker->setInterval(60); impworker = new ImageProcessingWorker(0, QString::fromUtf8(startx).toInt(), QString::fromUtf8(starty).toInt(), QString::fromUtf8(endx).toInt(), QString::fromUtf8(endy).toInt(), QString::fromUtf8(minArea).toInt(), QString::fromUtf8(maxArea).toInt(), 1, QString::fromUtf8(url)); // Соединяем сигнал started потока, со слотом process "рабочего" класса, т.е. начинается выполнение нужной работы. connect(dbthread, SIGNAL(started()), worker, SLOT(process())); connect(impthread, SIGNAL(started()), impworker, SLOT(process())); // По завершению выходим из потока, и удаляем рабочий класс connect(worker, SIGNAL(finished()), dbthread, SLOT(quit())); connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater())); connect(impworker, SIGNAL(finished()), impthread, SLOT(quit())); connect(impworker, SIGNAL(finished()), impworker, SLOT(deleteLater())); // Удаляем поток, после выполнения операции connect(dbthread, SIGNAL(finished()), dbthread, SLOT(deleteLater())); connect(impthread, SIGNAL(finished()), impthread, SLOT(deleteLater())); connect(impworker, SIGNAL(sendCounters(int,int, int)), worker, SLOT(waitForDB(int,int,int))); connect(impworker, SIGNAL(newCameraOpened(int)), this, SLOT(cameraStarted(int))); //connect(worker, SIGNAL(getDataSignal()), impworker, SLOT(prepareCounters())); worker->moveToThread(dbthread); impworker->moveToThread(impthread); dbthread->start(); impthread->start();
connect(impworker, SIGNAL(newCameraOpened(int)), this, SLOT(cameraStarted(int)));
void ImageProcessingWorker::process(){ qDebug() << "ImageProcessingWorker::process source = " << strSource; bool isRead = 0; cv::Mat imgFrame1; cv::Mat imgFrame2; cv::Mat imgFrame2Copy; blnFirstFrame = true; int frameCount = 2; try { capture.open(strSource.toStdString().c_str()); } catch(cv::Exception& e) { std::cerr << "Exception was catched!" << std::endl; cameraID = -1; emit newCameraOpened(cameraID); return; } if(!capture.isOpened()){ std::cerr << "Cannot open camera!" << std::endl; cameraID = -1; emit newCameraOpened(cameraID); return; } else { isRead = capture.read(imgFrame1); imageSize.height = capture.get(CV_CAP_PROP_FRAME_HEIGHT); imageSize.width = capture.get(CV_CAP_PROP_FRAME_WIDTH); fps = 25; cameraID = createNewDbLine(); emit newCameraOpened(cameraID); if(cameraID == -1) return; QString fName = QString::number(cameraID) + ".avi"; if(!writer.open(fName.toStdString().c_str(), CV_FOURCC('F', 'M', 'P', '4'), fps, imageSize)) { std::cerr << "Cannot write video!" << std::endl; capture.release(); cameraID = -1; return; } }