Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTemporaryFile is empty

I want to use temporary files in Qt project

I try this code:

QTemporaryFile file;
file.open();
QTextStream stream(&file);
stream << content; // content is a QString

qDebug() << file.readAll();

But console shows an empty string:

""

How can I write a string in a QTemporaryFile?

like image 204
Intelligide Avatar asked Nov 05 '25 14:11

Intelligide


1 Answers

Everything is working fine. QTemporaryFile always opens as ReadWrite, and is a random-access device, which means that after you write data, you either need to close and re-open it (which is an overkill), or go to the beginning of the file to read it:

QTemporaryFile file;
file.open();
QTextStream stream(&file);
stream << content; // here you write data into file. 
//Your current position in the file is at it's end, 
//so there is nothing for you to read.
stream.flush();//flush the stream into the file

file.seek(0); //go to the begining

qDebug() << file.readAll(); //read stuff
like image 113
SingerOfTheFall Avatar answered Nov 07 '25 12:11

SingerOfTheFall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!