QT.zipGuys,
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
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
To copy to clipboard, switch view to plain text mode
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>
{
public:
void tempo();
};
void Corrida::tempo()
{
connect(timer, SIGNAL(timeout()), this, SLOT(ler_arquivos()));
timer->start(10);
}
#endif // 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
To copy to clipboard, switch view to plain text mode
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
) : 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");
{
float cont1 = 0;
float cont2 = 0;
float cont3 = 0;
float cont4 = 0;
float porce = 0;
while (!in.atEnd()){
ui->tableWidget->setRowCount(linha);
if(QString::compare(fields.
at(1),
"executado")==0) {
cont1 +=1;
}
else if(QString::compare(fields.
at(1),
"erro")==0) {
cont2 +=1;
}
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Attached is the project file and the *.txt, I'm using for testing:
https://docs.google.com/file/d/0BwQ4...dit?usp=sharin
Bookmarks