void Connector::onTimeOut() { qDebug("on Time Out"); timer.stop(); stopRead(); startRead(1, 4); }
void Connector::startRead(qint8 start, qint8 num) { qDebug("start Read"); exceptByte = 5 + 48 * num; // 数据头+数据 qDebug()<<QString("start = %1, num = %2").arg(start).arg(num); stopRead(); connect(this, SIGNAL(readyRead()), this, SLOT(handleReadyRead())); connect(this, SIGNAL(readAllData()), this, SLOT(handleReadData())); this->start = start; this->num = num; callForData(start, num); }
void Connector::handleReadData() { qDebug()<<"Connector::handleReadData"; int dataStart = ba.indexOf(QByteArray(returnHead, 4), 0); if(dataStart != -1) { // 找到返回头 // 下一个是数据长度 if(dataStart + 4 >= ba.size()) { return; // 数据长度不够 } int dataLen = ba[dataStart + 4]; qDebug()<<dataStart + 5 + dataLen<<" "<<ba.size(); if(dataStart + 5 + dataLen > ba.size()) { return; // 数据长度不够 } else { emit readOK(ba.mid(dataStart + 6, dataLen)); //结束 stopRead(); } } else { callForData(start, num); } }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { thread = new QThread(); reader = new Reader(); reader->moveToThread(thread); connect(reader, SIGNAL(sendPoint(int,int,double)), this, SLOT(setPointProb(int,int,double))); connect(reader, SIGNAL(workRequested()), thread, SLOT(start())); connect(thread, SIGNAL(started()), reader, SLOT(doWork())); connect(reader, SIGNAL(finished()), thread, SLOT(quit()) , Qt::DirectConnection); connect(reader, SIGNAL(finished()), this, SLOT(fileFinished())); connect(reader, SIGNAL(sendReset()), this, SLOT(resetPositions())); setFixedSize(800,600); p_scene = new QGraphicsScene(this); p_view = new QGraphicsView(p_scene, this); QPixmap background(QCoreApplication::applicationDirPath() + "/img/sol.png"); if (background.isNull()) { std::cout << "Ouverture de sol.png échouée, ouverture de sol.jpg" << std::endl; background = QPixmap(QCoreApplication::applicationDirPath() + "/img/sol.jpg"); } p_scene->setBackgroundBrush(QBrush(background)); p_view->setGeometry(10,10,505,505); p_addPosition = new QPushButton("Ajouter position", this); p_addPosition->setGeometry(590,90, 120, 30); p_deletePos = new QPushButton("Réinitialiser positions", this); p_deletePos->setGeometry(590,130, 120, 30); p_openFile = new QPushButton("Ouvrir fichier", this); p_openFile->setGeometry(590, 170, 120, 30); p_changeTableSize = new QPushButton("Changer taille table", this); p_changeTableSize->setGeometry(50, 560, 120, 30); pauseButton = new QPushButton("", this); QIcon pauseIcon(QCoreApplication::applicationDirPath() + "/img/pause.png"); if (pauseIcon.isNull()) { std::cout << "Ouverture de pause.png échouée, ouverture de pause.jpg" << std::endl; pauseIcon = QIcon((QCoreApplication::applicationDirPath() + "/img/pause.jpg")); } pauseButton->setIcon(pauseIcon); pauseButton->setGeometry(590, 250, 30, 30); pauseButton->setDisabled(true); stopButton = new QPushButton("", this); QIcon stopIcon(QCoreApplication::applicationDirPath() + "/img/stop.png"); if (stopIcon.isNull()) { std::cout << "Ouverture de stop.png échouée, ouverture de stop.jpg" << std::endl; pauseIcon = QIcon((QCoreApplication::applicationDirPath() + "/img/stop.jpg")); } stopButton->setIcon(stopIcon); stopButton->setGeometry(630, 250, 30, 30); stopButton->setDisabled(true); stepButton = new QPushButton("", this); QIcon stepIcon(QCoreApplication::applicationDirPath() + "/img/step.png"); if (stepIcon.isNull()) { std::cout << "Ouverture de step.png échouée, ouverture de step.jpg" << std::endl; stepIcon = QIcon((QCoreApplication::applicationDirPath() + "/img/step.jpg")); } stepButton->setIcon(stepIcon); stepButton->setGeometry(670, 250, 30, 30); stepButton->setDisabled(true); p_quit = new QPushButton("Quitter", this); p_quit->setGeometry(600, 560, 100, 30); p_addPosX = new QLineEdit(this); p_addPosY = new QLineEdit(this); p_addPosP = new QLineEdit(this); p_addPosX->setGeometry(580, 50, 40, 30); p_addPosY->setGeometry(630, 50, 40, 30); p_addPosP->setGeometry(680, 50, 40, 30); p_addPosX->setPlaceholderText("Abscisse"); p_addPosY->setPlaceholderText("Ordonnée"); p_addPosP->setPlaceholderText("Proba"); currentDate = new QLabel("Pas de fichier en cours", this); currentDate->setGeometry(580, 300, 140, 30); QObject::connect(p_addPosition, SIGNAL(clicked()), this, SLOT(sendPointProbValues())); QObject::connect(this, SIGNAL(pointProbValues(int,int,double)), this, SLOT(setPointProb(int,int,double))); QObject::connect(p_deletePos, SIGNAL(clicked()), this, SLOT(resetPositions())); QObject::connect(p_quit, SIGNAL(clicked()), qApp, SLOT(quit())); QObject::connect(p_openFile, SIGNAL(clicked()), this, SLOT(readMatrixFromFile())); QObject::connect(p_changeTableSize, SIGNAL(clicked()), this, SLOT(changeTableSize())); QObject::connect(pauseButton, SIGNAL(clicked()), this, SLOT(pauseRead())); QObject::connect(stopButton, SIGNAL(clicked()), this, SLOT(stopRead())); QObject::connect(reader, SIGNAL(sendDate(QString)), this, SLOT(setDate(QString))); QObject::connect(stepButton, SIGNAL(clicked()), this, SLOT(clickNextDate())); }