Problem using QFile inside QThread
Hi,
I have this code inside the run() of a QThread:
Code:
outfile.setFileName(outputFile);
return;
outStream.setDevice(&outfile);
outStream << "Some data here!" << "\n";
outfile.close();
Both outfile and outStream are private members of the thread class:
When I ran it. The out file is created but it has 0 bytes.
Funny thing is that when I use QFile.Write() it works.
Any idea why?
Thanks,
Carlos.
Re: Problem using QFile inside QThread
what happens if you add flush() before you close the file?
Re: Problem using QFile inside QThread
The same thing happens: The file is created but QTextStream fail to move the data into the file.
If I copy the same code outside the thread class into a the main() for example, it works just fine.
Re: Problem using QFile inside QThread
Warning: Threads are not my strong field!
But maybe it has to do, that outfile and outStream are not in the same thread as the run() method because they are private members of the class? So try to declare and define them in the run method.
Re: Problem using QFile inside QThread
Quote:
If I copy the same code outside the thread class into a the main() for example, it works just fine.
Its hard to say without seeing more code.
Re: Problem using QFile inside QThread
The full code is:
sqlimport.h
Code:
{
Q_OBJECT
public:
void run(); //The main thread process
private:
};
sqlimport.cpp
Code:
void sqlimport::run()
{
outfile.setFileName(outputFile);
return;
outStream.setDevice(&outfile);
outStream << "Some data here!" << "\n";
outfile.close();
}
That's it. If you do:
Code:
sqlimport mysubproc;
mysubproc.outputFile = "/data/myfile.sql";
mysubproc.start(); //Run the thread
The output file is created but QTextStream fails to write the data.
Carlos.