PDA

View Full Version : Reading a file every second



Oz
4th January 2018, 00:07
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.



timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(getValue());
timer->start(1000);

...

void myProgram::getValue()
{
myFile = new QFile (filename);
abc = qnetworkaccessmanager.get(QNetworkRequest(url)); //abc is QNetworkReply
connect(abc, SIGNAL(readyRead()), this, SLOT(readValue()));
file->open(QIODevice::ReadWrite);
}

void myProgram::readValue()
{
if(file)
file->write(abc->readAll());
file->close(); // maybe not Necessary
file->open(QIODevice::ReadOnly);
if(file)
{
QTextStream in (file);
QString line = in.readLine();
for (int i = 0; i < numberSize(); i++)
{
switch(i)
{
case 0:
{ ListofValues.append(line.section(',', 8-i, 8-i)); //ListofValues is QList <QString>
ui->lcdNumber->display(ListofValues[i]);
break;
}
case 1: ... same till case 8;
}
}
}//end if
file.close();
}



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

d_stranz
4th January 2018, 00:47
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.

Oz
4th January 2018, 02:02
Thanks d_stranz for pointing those things out.

I do have a code for deleting the file instances
delete file; file = 0; after I'm done with the cases.

something like

case 8:
{ ListofValues.append(line.section(',', 8-i, 8-i));
ui->lcdNumber->display(ListofValues[i]);
break;
}
}//switch
}//for
}//if
delete file;
file = 0;
abc->deleteLater();//added after your comment
abc = 0; // added after your comment
}


I also tried to instantiate Qfile in the constructor but it would give me a SEG FAULT pointing to getCurrent()


file->open(QIODevice::ReadWrite);


what do you mean to connect the abc again to the same SLOT?
something like this?

void myProgram::getValue()
{
myFile = new QFile (filename);
abc = qnetworkaccessmanager.get(QNetworkRequest(url));
connect(abc, SIGNAL(readyRead()), this, SLOT(readValue()));
connect(abc, SIGNAL(readyRead()), this, SLOT(readValue()));
file->open(QIODevice::ReadWrite);
}

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:


for (int i = 0; i < numberSize(); i++)
{
switch(i)
{
case 0:
{ ListofValues.append(line.section(',', 8-i, 8-i));
ui->lcdNumber->display(ListofValues[i]);
break;
}
//=====pseudo code explanation
for i= 0;
then 8-i // gets the ninth number in my file {1,2,3,4,5,6,7,8,9}
ListofValues[i]// index 0 = 9
ui->display(ListofValues[i])// display number 9 in the GUI
for i=1;
then 8-i //get the eighth number
ListofValues[i] // = 8
ui->display(ListofValues[i])// display number 8 in the GUI
for i=2;
8-i//gets the seventh and so on.
//======

Lesiok
4th January 2018, 07:49
Show the declaration of file and myFile.

Oz
4th January 2018, 17:05
Show the declaration of file and myFile.

The declaration of file is in the header file

QFile *file;
QFileInfo *fileInfo;
//and other members explained previously

myFile is literally just a single string of 9-10 numbers separated by commas


//myFile.txt this comment is not in the file
1,2,3,4,5,6,7,8,9

Lesiok
4th January 2018, 18:31
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.

d_stranz
4th January 2018, 19:29
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.

Oz
4th January 2018, 19:41
Yes sorry that was a typo
I meant to say


QFile *myFile;


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.

d_stranz
4th January 2018, 23:33
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.