while (1){ .... emit progress(); ....}
void Client::createThread(LongAction *action){ QThread *newThread = new QThread(this); action->moveToThread(newThread); connect(action, SIGNAL(setRange(int, int)), SLOT(setRangeForProgressBar(int, int))); connect(action, SIGNAL(progress(int)), SLOT(setValueForProgressBar(int))); connect(action, SIGNAL(addToLog(QString, bool)), SLOT(addLog(QString, bool))); connect(action, SIGNAL(finished(QString)), SLOT(structureDownloadFinished(QString))); newThread->connect(action, SIGNAL( finished() ), SLOT( quit() )); newThread->start();}
void DownloadAction::readResponse(){ QTcpSocket *clientSocket = (QTcpSocket*)sender(); QDataStream in(clientSocket); for (;;) { if (!_nextBlockSize) { if (clientSocket->bytesAvailable() < sizeof(quint32)) break; in >> _nextBlockSize; } if (clientSocket->bytesAvailable() < _nextBlockSize) break; if (!_responseCode) { in >> _responseCode; QString info; QString fileName; int range = 0; if (_responseCode == 150) { in >> fileName >> _fileSize >> range; emit setRange(_rowProgressBar, range); if (!QFile::exists("download_files")) { QDir dir(QDir::currentPath()); dir.mkdir("download_files"); } QString filePath = QDir::currentPath() + "/download_files/" + fileName; _receiveFile->setFileName(filePath); if (!_receiveFile->open(QFile::WriteOnly)) { emit addToLog("Error open file " + _receiveFile->fileName() + " : " + _receiveFile->errorString(), true); return; } _nextBlockSize = 0; continue; } else if (_responseCode == 450) { in >> info; continue; } QString response = "Response: " + QString::number(_responseCode); if (!info.isEmpty()) response += ": " + info; emit addToLog(response); } char data[_nextBlockSize]; int outData = in.readRawData(data, sizeof(char) * _nextBlockSize); _receiveFile->write(data, sizeof(char) * outData); _receivingBytes += outData; emit progress(_rowProgressBar); _nextBlockSize = 0; if (_receivingBytes == _fileSize) { emit addToLog("File " + _receiveFile->fileName() + " received"); _receiveFile->close(); emit finished(); return; } }}
C++ (Qt)newThread->connect(action, SIGNAL( finished() ), SLOT( quit() ));
C++ (Qt)connect(action, SIGNAL( finished() ), newThread, SLOT( quit() ));