How we can open a png file with QFile and then save it with new name......
-
How we can open a png file with QFile and then save it with new name......
for file text i had not any problem but for binary file it can not read all data corecty.........
for example for read a png file i read.......QByteArray binaryFile= file.readAll(); QString SerialOutputFile; if(ui->lineEditSerialOutputDirectory->text().isEmpty()) { SerialOutputFile =QDir::currentPath()+"/SerialOutputFile.png"; }else{ SerialOutputFile =ui->lineEditSerialOutputDirectory->text()+"/SerialOutputFile.png"; } //QString SerialOutputFile =QDir::currentPath()+"SerialOutputFile.png"; QFile outputFile(SerialOutputFile); outputFile.open(QIODevice::WriteOnly); QString str =binaryFile; if(!outputFile.isOpen()) { //alert that file did not open } QTextStream outStream(&outputFile); outStream << str; outputFile.close(); binaryFile.clear(); //open text file to user QDesktopServices::openUrl(QUrl(SerialOutputFile));
output only is...........but png is 4kbyte...........
?PNG
-
@stackprogramer
your problem is probably here:
QTextStream outStream(&outputFile);
QTextstream expects text comfort chars Use a QDataStream instead.
That said, did you know about QFiles static
functions ?
https://doc.qt.io/qt-5/qfile.html#rename-1
https://doc.qt.io/qt-5/qfile.html#copy-1which can both be used to rename a file
-
@J-Hilk
I used QDatastream but......size of input png is 4564byte and output is 4568....
png file is not open..........my main problem is open png file.......if (!file.open(QIODevice::ReadOnly)) { qDebug() << file.errorString(); } QByteArray binaryFile= file.readAll(); qDebug()<<binaryFile.size(); QString SerialOutputFile; if(ui->lineEditSerialOutputDirectory->text().isEmpty()) { SerialOutputFile =QDir::currentPath()+"/SerialOutputFile.png"; }else{ SerialOutputFile =ui->lineEditSerialOutputDirectory->text()+"/SerialOutputFile.png"; } //QString SerialOutputFile =QDir::currentPath()+"SerialOutputFile.txt"; QFile outputFile(SerialOutputFile); outputFile.open(QIODevice::WriteOnly); QString str =binaryFile; if(!outputFile.isOpen()) { //alert that file did not open } QDataStream outStream(&outputFile); outStream << binaryFile; outputFile.close(); binaryFile.clear(); //open text file to user QDesktopServices::openUrl(QUrl(SerialOutputFile)); qDebug()<<outputFile.size();
-
I found solution my problem is solved.......
Problem: QDataStream uses the "<<" method to write data with extra characters, such as hexadecimal notation "0000 001a"
Solution: QDataStream uses writeRawData() to write data to avoid extra bytes.
more info https://www.programmersought.com/article/1716209297/QFile file("file.txt"); file.open(QIODevice::WriteOnly); QDataStream out(&file); // we will serialize the data into the file / / original code / / Use the operator input will make out with invalid characters, the output is 30 //out << QString("the answer is"); // serialize a string / / changed code / / Output results are 14, no invalid characters out.writeRawData("the answer is",sizeof("the answer is")); qDebug()<<"length : "<<file.size(); file.close(); exit(1);
-
How we can open a png file with QFile and then save it with new name
I don't understand why you are using stream anything, if all you are doing is calling
writeRawData()
.To write raw bytes straight to a file opened with
QFile
you can just use QIODevice::write.But if you are just copying a
png
file to a new name, as your question states, then @J-Hilk 's[static]bool QFile::copy(const QString &fileName, const QString &newName)
is as simple & efficient as it gets....