Results 1 to 9 of 9

Thread: Reading a file every second

  1. #1
    Join Date
    Dec 2017
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Reading a file every second

    I am having trouble troubleshooting this problem.

    I have a QTimer that calls a slot every second.
    The SLOT sends a request to server then receives the html or text and then it gets written to a document.
    THen I'm reading the info of this document and pass the values to variables that end up being displayed in a GUI.

    Qt Code:
    1. timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(getValue());
    3. timer->start(1000);
    4.  
    5. ...
    6.  
    7. void myProgram::getValue()
    8. {
    9. myFile = new QFile (filename);
    10. abc = qnetworkaccessmanager.get(QNetworkRequest(url)); //abc is QNetworkReply
    11. connect(abc, SIGNAL(readyRead()), this, SLOT(readValue()));
    12. file->open(QIODevice::ReadWrite);
    13. }
    14.  
    15. void myProgram::readValue()
    16. {
    17. if(file)
    18. file->write(abc->readAll());
    19. file->close(); // maybe not Necessary
    20. file->open(QIODevice::ReadOnly);
    21. if(file)
    22. {
    23. QTextStream in (file);
    24. QString line = in.readLine();
    25. for (int i = 0; i < numberSize(); i++)
    26. {
    27. switch(i)
    28. {
    29. case 0:
    30. { ListofValues.append(line.section(',', 8-i, 8-i)); //ListofValues is QList <QString>
    31. ui->lcdNumber->display(ListofValues[i]);
    32. break;
    33. }
    34. case 1: ... same till case 8;
    35. }
    36. }
    37. }//end if
    38. file.close();
    39. }
    To copy to clipboard, switch view to plain text mode 


    Now if I refresh the file in a text editor I can see the values change, BUT in my GUI the values don't change they only get the values the very first time the program is called. After that the values don't change but remain the same.

    Please any feedback is greatly appreciated I've spent a lot of time on this already.

    //sample of file is
    1,2,3,4,5,6,7,8,9
    so I get the 9 and put it in the first element in Qlist < QString> and so on

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,997
    Thanks
    232
    Thanked 632 Times in 622 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading a file every second

    So many things wrong, it is hard to know where to start. Not to mention that the code you posted won't compile because of the typos in it... why do people think they have to invent code to post that is almost but not quite the same as the real code they are trying to debug?

    First, why are you creating a new QFile instance every time the timer fires? You never delete any of them, so you will keep accumulating QFile instances until you eventually run out of memory. Create one, once (maybe in the class constructor) and simply reuse it.

    Second, if the timer fires twice before the network reply is received, then you'll overwrite the first QFile and QNetworkReply instances with new values, connect the new "abc" to the same slot again, and will likely have the slot fire twice, once for the original request and again for the second one.

    Likewise, the QNetworkReply instance returned by QNetworkAccessManager::get() is also new memory and has to be deleted when you are done using it.

    And because you never delete the "abc" instances, the connections made to the slots also never go away either.

    Finally, because you have left out almost all of the code in the for() loop where you actually display the data you read, it is impossible to tell if the error occurs in that part of the code or somewhere else. The fact that you have a switch() statement that switches on the loop index implies you really don't understand the logic behind what you are trying to do, especially when the expressions "8 - i" and ListofValues[i] don't really have anything to do with case "0" unless the other cases do something completely different.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2017
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading a file every second

    Thanks d_stranz for pointing those things out.

    I do have a code for deleting the file instances
    Qt Code:
    1. delete file; file = 0;
    To copy to clipboard, switch view to plain text mode 
    after I'm done with the cases.

    something like
    Qt Code:
    1. case 8:
    2. { ListofValues.append(line.section(',', 8-i, 8-i));
    3. ui->lcdNumber->display(ListofValues[i]);
    4. break;
    5. }
    6. }//switch
    7. }//for
    8. }//if
    9. delete file;
    10. file = 0;
    11. abc->deleteLater();//added after your comment
    12. abc = 0; // added after your comment
    13. }
    To copy to clipboard, switch view to plain text mode 

    I also tried to instantiate Qfile in the constructor but it would give me a SEG FAULT pointing to getCurrent()
    Qt Code:
    1. file->open(QIODevice::ReadWrite);
    To copy to clipboard, switch view to plain text mode 

    what do you mean to connect the abc again to the same SLOT?
    something like this?
    Qt Code:
    1. void myProgram::getValue()
    2. {
    3. myFile = new QFile (filename);
    4. abc = qnetworkaccessmanager.get(QNetworkRequest(url));
    5. connect(abc, SIGNAL(readyRead()), this, SLOT(readValue()));
    6. connect(abc, SIGNAL(readyRead()), this, SLOT(readValue()));
    7. file->open(QIODevice::ReadWrite);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Last but not least, the code in the for-switch is to put the values 9-0 into a list of the same size. ex:
    Qt Code:
    1. for (int i = 0; i < numberSize(); i++)
    2. {
    3. switch(i)
    4. {
    5. case 0:
    6. { ListofValues.append(line.section(',', 8-i, 8-i));
    7. ui->lcdNumber->display(ListofValues[i]);
    8. break;
    9. }
    10. //=====pseudo code explanation
    11. for i= 0;
    12. then 8-i // gets the ninth number in my file {1,2,3,4,5,6,7,8,9}
    13. ListofValues[i]// index 0 = 9
    14. ui->display(ListofValues[i])// display number 9 in the GUI
    15. for i=1;
    16. then 8-i //get the eighth number
    17. ListofValues[i] // = 8
    18. ui->display(ListofValues[i])// display number 8 in the GUI
    19. for i=2;
    20. 8-i//gets the seventh and so on.
    21. //======
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,446
    Thanked 270 Times in 265 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading a file every second

    Show the declaration of file and myFile.

  5. #5
    Join Date
    Dec 2017
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading a file every second

    Quote Originally Posted by Lesiok View Post
    Show the declaration of file and myFile.
    The declaration of file is in the header file
    Qt Code:
    1. QFile *file;
    2. QFileInfo *fileInfo;
    3. //and other members explained previously
    To copy to clipboard, switch view to plain text mode 

    myFile is literally just a single string of 9-10 numbers separated by commas
    Qt Code:
    1. //myFile.txt this comment is not in the file
    2. 1,2,3,4,5,6,7,8,9
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,446
    Thanked 270 Times in 265 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading a file every second

    myFile is a pointer to QFile object (line 9 in first post).
    You initiate the myFile pointer (line 9) and then use the file pointer everywhere (line 12 too). I don't see where file pointer is initiated.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,997
    Thanks
    232
    Thanked 632 Times in 622 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading a file every second

    myFile is a pointer to QFile object (line 9 in first post).
    If @Oz would post his actual, complete, compilable code it would go a long way to helping us solve the problem. Deciding to post just bits and pieces of "code' and "pseudocode" doesn't help at all.

    But if we have to continue to pull teeth one at a time at some point we'll just decide that @Oz doesn't really want to help us help him and we'll give up.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Dec 2017
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading a file every second

    Yes sorry that was a typo
    I meant to say
    Qt Code:
    1. QFile *myFile;
    To copy to clipboard, switch view to plain text mode 

    My first post is myprogram.cpp

    the declaration of myFile is inside myprogram.h

    Also I got to the program running correctly now. I guess the monster was lurking around that for-switch. I simply got rid of it.

    Thanks everybody for all the assistance.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,997
    Thanks
    232
    Thanked 632 Times in 622 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reading a file every second

    My first post is myprogram.cpp

    the declaration of myFile is inside myprogram.h
    No, your first post contained an incomplete, uncompilable fragment of a source code file. And nowhere did you ever post the header file or code from it that defines your myprogram class and the methods and member variables in it.

    Look, it's great that you got your program working on your own, but you wasted a lot of our time in reading your posts, responding to your posts, and trying to get you to post more information. If you want help in the future with Qt coding problems, then you need to post all of the code that isn't working so that we can study it and maybe actually give you some help. If you only post what you think is necessary to present the problem, then more often than not what you post will be useless. As you said, the problem in your code turned out to be in the code you didn't post.

    If you had posted all of your code in the first place, we probably could have pointed directly to the problem in the first response instead of wasting several days going in circles with you.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 19th January 2016, 09:57
  2. reading pdf file
    By kito in forum Newbie
    Replies: 8
    Last Post: 23rd June 2012, 08:17
  3. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 18:55
  4. reading a file
    By xaqt in forum Newbie
    Replies: 6
    Last Post: 14th September 2009, 15:04
  5. reading from a file
    By mickey in forum General Programming
    Replies: 32
    Last Post: 19th July 2007, 01:04

Tags for this Thread

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.