Why not read the file again?

Good afternoon. Such problem: the function opens the file, creates a thread, reads from it a string and returns. Data are recorded in a TreeWidget. And so to the end. Then the file is closed.
Created a button in the app, which first clears TreeWidget (tested, all works correctly), and the second calls the function above.
When the application is started, all normal reads and writes in the TreeWidget. Then I open the file in which the data lie, change a couple of lines, save and close. Press the button and nothing happens. When you launch the app again, all changes become visible. Please help. Here is the code:
Function:
if(!m_stream.atEnd())
{
m_stream.setCodec("utf-8");
 QString line = m_stream.readLine();

 return line;
}
 return "";

m_stream is created in another function, there is bound to the file.
As I said above, after the completion of this function, the file I close.
April 19th 20 at 12:32
1 answer
April 19th 20 at 12:34
Solution
stream will read the file from the beginning only if it re-open, or call seek(0);
PS: Keep an open file is a bad practice.
The file open until then, until the entire read. Then I close the file.
Then I change the contents of the file, save and close. When you press the button, the file is opened again, a thread is created new, but somehow it reads the old data... - neoma.Bauch80 commented on April 19th 20 at 12:37
@neoma.Bauch80and the object m_stream recreate it? - Elise68 commented on April 19th 20 at 12:40
@Elise68, it Turns out the m_stream object initialized in class. Creates a new object, passing it the file name, the file name is assigned to the QFile object, the file is opened and the object m_stream attached file.
Here is the full code:
FileManager::FileManager(const QString file_name)
{
 m_file_name = file_name;
}
bool FileManager::open_file_for_read()
{
m_file.setFileName(m_file_name);
if(!m_file.open(QIODevice::ReadOnly))
 return false;
 return true;
}
void FileManager::creatStream()
{
m_stream.setDevice(&m_file);
}
QString FileManager::read_file()
{
if(!m_stream.atEnd())
{
m_stream.setCodec("utf-8");
 QString line = m_stream.readLine();

 return line;
}
 return "";
}
void FileManager::close_file()
{
m_file.close();
}

File .h:
//...
protected:
 QFile m_file;
 QString m_file_name;
 QTextStream m_stream;
- neoma.Bauch80 commented on April 19th 20 at 12:43
@neoma.Bauch80, as far as I know QTextStream holds a pointer to QIODevice and position. Probably putting back the new file does not throw position. Try it with a job file to call m_stream.seek(0); - Elise68 commented on April 19th 20 at 12:46
@Elise68does Not work... Added in the constructor, and before binding and after binding, and before opening a file and after... - neoma.Bauch80 commented on April 19th 20 at 12:49
@Elise68, In General the problem is solved just to ugliness. My file was in the resource file .qrc. And just because this was such a problem. Deleted the file from file .qrc and it worked! And You still thanks for the help :) - neoma.Bauch80 commented on April 19th 20 at 12:52

Find more questions by tags Qt