class Thread_of_pdf : public QThread{ Q_OBJECTpublic: Thread_of_pdf (QStringList List_param, QObject *parent); void run();signals: void write_file_fin (); void down_label (QString n); void set_value_prog (int b); void start_trans(); void close_pro (); void abort_con();private : QStringList param_of_connection;};
Thread_of_pdf::Thread_of_pdf (QStringList List_param, QObject *parent){ param_of_connection = List_param;}void Thread_of_pdf::run(){ QString address = param_of_connection.at(0); QString Port = param_of_connection.at(1); QString word = param_of_connection.at(2); Client_pdf *qw = new Client_pdf(address ,Port,word ) ; // вот тут креш, даже не создает обьект connect (qw, SIGNAL(destroyed()), qw, SLOT(deleteLater())); connect(qw, SIGNAL(set_value_prog(int)),this, SIGNAL(set_value_prog(int))); connect (qw, SIGNAL(close_pro()), this , SIGNAL(close_pro())); connect (qw, SIGNAL(start()), this, SIGNAL(start_trans())); connect (qw, SIGNAL(write_file_fin()), this, SIGNAL(write_file_fin())); connect (qw, SIGNAL(down_label(QString)),this, SIGNAL(down_label(QString))); connect (this, SIGNAL(abort_con()),qw, SLOT(abort_send()));}
class Client_pdf : public QDialog { Q_OBJECT public: Client_pdf(QString &str_port , QString &str_adress , QString &key_ret ); struct pol { QString adr ; QString port_p ; QString key_pv ; }; struct tab { QStringList ex_word; QStringList ex_path; }; private: QTcpSocket *tcpSock_pdf; quint16 blockSize; int socketDescriptor; QString fer, str_port, str_adress; QString key_ber ; pol NS ; //QString receive_text; QByteArray ter; //bool ind_send_mes; //QTimer *ret; quint64 rtg; qint64 nextBlockSize; bool qw; qint64 y; //QString file_type; private slots: //void request(); void read (); void displayError(QAbstractSocket::SocketError socketError); void send_rec_v(); void abort_send(); signals : void write_file_fin (); //void up_label (QString m); void down_label (QString n); void set_value_prog (int b); void start(); void max_value(int); void close_pro (); };
Client_pdf::Client_pdf(QString &str_adress,QString &str_port, QString &key_ret) // : QDialog(parent) { qw = false; rtg = 0; NS.adr = str_adress ; NS.port_p = str_port; NS.key_pv = key_ret ; qDebug ()<< NS.adr <<" " << NS.port_p << " " << NS.key_pv ; // find out which IP to connect to key_ber = key_ret ; QString ipAddress; QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); // use the first non-localhost IPv4 address for (int i = 0; i < ipAddressesList.size(); ++i) { if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) ipAddress = ipAddressesList.at(i).toString(); } // if we did not find one, use IPv4 localhost if (ipAddress.isEmpty()) ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); //ter.append("FW"); tcpSock_pdf = new QTcpSocket(this); connect (tcpSock_pdf ,SIGNAL(connected()) , this , SLOT(send_rec_v())); connect (tcpSock_pdf, SIGNAL(readyRead()),this, SLOT(read())); connect(tcpSock_pdf, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError))); connect (tcpSock_pdf,SIGNAL(disconnected()),tcpSock_pdf,SLOT(deleteLater())); //connect(ret, SIGNAL(timeout()),this, SLOT(send_sig_em())); blockSize = 0; tcpSock_pdf->abort(); tcpSock_pdf->connectToHost(NS.adr, NS.port_p.toInt());}void Client_pdf::read(){ int prog_val; QString bc; bc = QDir::currentPath(); bc.append("/more_m/"); QStringList ert = NS.key_pv.split("/"); bc.append(ert.last()); QString lab_down = NS.key_pv;QFile file (bc); QDataStream in(tcpSock_pdf); in.setVersion(QDataStream::Qt_4_0);if (qw == false) { emit down_label(lab_down); emit start(); in >> y;// emit max_value (y); qDebug() << " YYYY " << y; qw= true;} if (blockSize == 0) { if (tcpSock_pdf->bytesAvailable() < (int)sizeof(quint16)) return; } while (file.size()<y) { tcpSock_pdf->waitForReadyRead(); int ret; if (tcpSock_pdf->bytesAvailable()) { //QFile file(bc); if (file.open(QIODevice::Append)) { QByteArray buf = tcpSock_pdf->readAll();//читаем все данные из сокета if (buf.size()) {//если данные считаны //qDebug() << "Buff SIZE" << buf.size() << "FILE SIZE " << file.size(); file.write(buf);//записываем байты } qDebug() << "tcpSock_pdf->bytesAvailable() " << tcpSock_pdf->bytesAvailable() << " FILE SIZE " << file.size(); file.close(); tcpSock_pdf->flush(); ret = ((int)file.size()/(y/100)); qDebug() << y<<" Progress value " << ret; } } if (prog_val!=ret) { emit this->set_value_prog(ret); prog_val = ret; }} tcpSock_pdf->disconnectFromHost();}void Client_pdf::displayError(QAbstractSocket::SocketError socketError){ switch (socketError) { case QAbstractSocket::RemoteHostClosedError: {// emit write_file_fin (); break; } case QAbstractSocket::HostNotFoundError: QMessageBox::information(this, tr("DemAsTPro"), tr("The host was not found. Please check the " "host name and port settings.")); break; case QAbstractSocket::ConnectionRefusedError: QMessageBox::information(this, tr("DemAsTPro"), tr("The connection was refused by the peer. " "Make sure the fortune server is running, " "and check that the host name and port " "settings are correct.")); break; default: QMessageBox::information(this, tr("DemAsTPro"), tr("The following error occurred: %1.") .arg(tcpSock_pdf->errorString())); }}void Client_pdf::send_rec_v(){ QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << (quint16)0; out << NS.key_pv; out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); tcpSock_pdf->write(block); tcpSock_pdf->flush(); tcpSock_pdf->waitForReadyRead() ;}
mythread::run(){//init();bool ok = exec();// ВНИМАНИЕ когда вызывается QThread::quit() из другого потока у меня почему-то сюда выполнение не доходит.}
void Client_pdf::abort_send(){ QTcpSocket *tcpSock_p = new QTcpSocket(); if (!tcpSock_p->setSocketDescriptor(socketDescriptor)) { emit error(tcpSock_p->error()); return; } tcpSock_p->disconnectFromHost(); QFile vc(NS.key_pv); if (vc.exists()) { if(vc.remove()== true) { emit this->error_download_file(); //сигнал ловится в основном потоке и пишется ошибка "передача прервана" return; } } emit close_pro(); tcpSock_p->deleteLater();} // дальше после выхода с слота просто вылетает // в QApplication Output выводит ASSERT failure in QWidget: "Widgets must be created in the GUI thread.", //filekernel\qwidget.cpp, line 1133