Results 1 to 3 of 3

Thread: Threads, Timer and Update a Field

  1. #1
    Join Date
    Sep 2013
    Posts
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Lightbulb Threads, Timer and Update a Field

    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:

    Qt Code:
    1. 192.168.0.1 executado OK
    2. 192.168.0.2 erro Sem_Acesso
    3. 192.168.0.3 executado OK
    4. 192.168.0.4 executado OK
    5. 192.168.0.5 executado OK
    6. 192.168.0.6 executado OK
    7. 192.168.0.7 executado OK
    8. 192.168.0.8 executado OK
    9. 192.168.0.9 executado OK
    10. 192.168.0.10 executado OK
    11. 192.168.0.11 executado OK
    12. 192.168.0.12 erro Sem_Acesso
    13. 192.168.0.13 erro Não_Responde_na_Rede
    14. 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:
    Qt Code:
    1. #ifndef CORRIDA_H
    2. #define CORRIDA_H
    3. #include <QThread>
    4. #include <QTimer>
    5.  
    6. class Corrida : public QThread
    7. {
    8. public:
    9. void tempo();
    10. };
    11.  
    12.  
    13. void Corrida::tempo()
    14. {
    15. QTimer *timer;
    16. timer = new QTimer(this);
    17. connect(timer, SIGNAL(timeout()), this, SLOT(ler_arquivos()));
    18. timer->start(10);
    19. }
    20.  
    21.  
    22. #endif // CORRIDA_H
    To copy to clipboard, switch view to plain text mode 

    mainwindows.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "Corrida.h"
    4. #include <QStandardPaths>
    5. #include <QFile>
    6. #include <QMessageBox>
    7. #include <QTextStream>
    8. #include <QDir>
    9. #include <QCoreApplication>
    10. #include <QDebug>
    11. #include <QStandardItem>
    12.  
    13. int coluna =0;
    14. int linha = 1;
    15.  
    16. void ler_arquivos();
    17.  
    18. MainWindow::MainWindow(QWidget *parent) :
    19. QMainWindow(parent),
    20. ui(new Ui::MainWindow)
    21. {
    22. ui->setupUi(this);
    23.  
    24. Corrida tempo;
    25. ler_arquivos();
    26. tempo.start();
    27. tempo.wait();
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. delete ui;
    33. }
    34. void MainWindow::ler_arquivos()
    35. {
    36. QFile inputFile("C:\\log_instalador.txt");
    37. if(inputFile.open(QIODevice::ReadOnly))
    38. {
    39. float cont1 = 0;
    40. float cont2 = 0;
    41. float cont3 = 0;
    42. float cont4 = 0;
    43. float porce = 0;
    44.  
    45.  
    46. QTextStream in (&inputFile);
    47. while (!in.atEnd()){
    48.  
    49. ui->tableWidget->setRowCount(linha);
    50. QString line = in.readLine();
    51. QStringList fields = line.split(" ");
    52. QString myS = fields.at(0);
    53. QString mySt = fields.at(1);
    54. QString myStr = fields.at(2);
    55.  
    56. if(QString::compare(fields.at(1),"executado")==0)
    57. {
    58. cont1 +=1;
    59. }
    60. else if(QString::compare(fields.at(1),"erro")==0)
    61. {
    62. cont2 +=1;
    63. }
    64. ui->tableWidget->setItem(coluna,0,new QTableWidgetItem(myS));
    65. ui->tableWidget->setItem(coluna,1,new QTableWidgetItem(mySt));
    66. ui->tableWidget->setItem(coluna,2,new QTableWidgetItem(myStr));
    67. coluna += 1;
    68. linha += 1;
    69. ui->tableWidget->resizeColumnsToContents(); //Dimenciona a celulas
    70. cont3 += 1;
    71. }
    72.  
    73. porce = ((cont1/cont3)*100);
    74. ui->Campo_Verde->setText(QString::number(porce,'f', 0)+" % Executado");
    75. porce = ((cont2/cont3)*100);
    76. ui->Campo_Vermelho->setText(QString::number(porce,'f', 0)+" % com Erro");
    77. porce = ((cont1+cont2)-cont3);
    78. ui->Campo_Amarelo->setText(QString::number(porce,'f', 0)+" % Resta");
    79. porce = (((cont1/cont3)*100)+((cont2/cont3)*100));
    80. ui->Campo_Status->setText(QString::number(porce,'f', 0)+" % Executado");
    81.  
    82.  
    83. qDebug()<< "Quantidade de Executado";
    84. qDebug()<< cont1;
    85. qDebug()<< "Quantidade de Erro";
    86. qDebug()<< cont2;
    87. qDebug()<< "Quantidade total outros";
    88. qDebug()<< cont4;
    89. qDebug()<< "Quantidade total de Linha";
    90. qDebug()<< cont3;
    91. }
    92. inputFile.close();
    93. }
    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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Threads, Timer and Update a Field

    start with this line, try to figure out what is wrong with it:
    Qt Code:
    1. connect(timer, SIGNAL(timeout()), this, SLOT(ler_arquivos()));
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 1st October 2013 at 20:54. Reason: missing [code] tags

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads, Timer and Update a Field

    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.

Similar Threads

  1. Update GUI from another object with 2 threads.
    By jiapei100 in forum Qt Programming
    Replies: 8
    Last Post: 19th February 2013, 17:27
  2. QDataWidgetMapper doesn't always update field in table
    By marcvanriet in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2011, 11:57
  3. How to update BLOB field in a SQLite database ?
    By aircraftstories in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2011, 20:45
  4. How to update GUI promptly with data sent from threads
    By Eos Pengwern in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2010, 11:33
  5. Replies: 2
    Last Post: 8th April 2010, 16:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.