PDA

View Full Version : Threads, Timer and Update a Field



lima_will
1st October 2013, 20:57
9661Guys,

I need help is the first time I'm working with threads and believe I'm doing something wrong. I have a file with the following structure:


192.168.0.1 executado OK
192.168.0.2 erro Sem_Acesso
192.168.0.3 executado OK
192.168.0.4 executado OK
192.168.0.5 executado OK
192.168.0.6 executado OK
192.168.0.7 executado OK
192.168.0.8 executado OK
192.168.0.9 executado OK
192.168.0.10 executado OK
192.168.0.11 executado OK
192.168.0.12 erro Sem_Acesso
192.168.0.13 erro Não_Responde_na_Rede
192.168.0.14 executado OK

This file may vary in size, so I'm using the timer and threads, because my idea makes it a first reading and shows on the screen.
After'm trying to use a thread to run in parallel and a timer to wait a few seconds. But I can not do this update.
Following codes:

Corrida.h:

#ifndef CORRIDA_H
#define CORRIDA_H
#include <QThread>
#include <QTimer>

class Corrida : public QThread
{
public:
void tempo();
};


void Corrida::tempo()
{
QTimer *timer;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(ler_arquivos()));
timer->start(10);
}


#endif // CORRIDA_H

mainwindows.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Corrida.h"
#include <QStandardPaths>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <QDir>
#include <QCoreApplication>
#include <QDebug>
#include <QStandardItem>

int coluna =0;
int linha = 1;

void ler_arquivos();

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

Corrida tempo;
ler_arquivos();
tempo.start();
tempo.wait();
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ler_arquivos()
{
QFile inputFile("C:\\log_instalador.txt");
if(inputFile.open(QIODevice::ReadOnly))
{
float cont1 = 0;
float cont2 = 0;
float cont3 = 0;
float cont4 = 0;
float porce = 0;


QTextStream in (&inputFile);
while (!in.atEnd()){

ui->tableWidget->setRowCount(linha);
QString line = in.readLine();
QStringList fields = line.split(" ");
QString myS = fields.at(0);
QString mySt = fields.at(1);
QString myStr = fields.at(2);

if(QString::compare(fields.at(1),"executado")==0)
{
cont1 +=1;
}
else if(QString::compare(fields.at(1),"erro")==0)
{
cont2 +=1;
}
ui->tableWidget->setItem(coluna,0,new QTableWidgetItem(myS));
ui->tableWidget->setItem(coluna,1,new QTableWidgetItem(mySt));
ui->tableWidget->setItem(coluna,2,new QTableWidgetItem(myStr));
coluna += 1;
linha += 1;
ui->tableWidget->resizeColumnsToContents(); //Dimenciona a celulas
cont3 += 1;
}

porce = ((cont1/cont3)*100);
ui->Campo_Verde->setText(QString::number(porce,'f', 0)+" % Executado");
porce = ((cont2/cont3)*100);
ui->Campo_Vermelho->setText(QString::number(porce,'f', 0)+" % com Erro");
porce = ((cont1+cont2)-cont3);
ui->Campo_Amarelo->setText(QString::number(porce,'f', 0)+" % Resta");
porce = (((cont1/cont3)*100)+((cont2/cont3)*100));
ui->Campo_Status->setText(QString::number(porce,'f', 0)+" % Executado");


qDebug()<< "Quantidade de Executado";
qDebug()<< cont1;
qDebug()<< "Quantidade de Erro";
qDebug()<< cont2;
qDebug()<< "Quantidade total outros";
qDebug()<< cont4;
qDebug()<< "Quantidade total de Linha";
qDebug()<< cont3;
}
inputFile.close();
}

Attached is the project file and the *.txt, I'm using for testing:

https://docs.google.com/file/d/0BwQ4G9j5uY1Jek90ZVVXRmdReWM/edit?usp=sharin

stampede
1st October 2013, 21:53
start with this line, try to figure out what is wrong with it:


connect(timer, SIGNAL(timeout()), this, SLOT(ler_arquivos()));

Lesiok
2nd October 2013, 09:05
1. As stampede said You don't have a slot ler_arquivos() in classs Corrida.
2. Method Corrida::tempo() is never start.
3. Class Corrida is unnecessary. Read about QFileSystemWatcher and use it instead of QTimer.