Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How we can open a png file with QFile and then save it with new name......

    General and Desktop
    3
    5
    329
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stackprogramer
      stackprogramer last edited by stackprogramer

      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
      
      
      
      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @stackprogramer last edited by

        @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-1

        which can both be used to rename a file

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 5
        • stackprogramer
          stackprogramer last edited by

          @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.......png not show

          
           
                            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();
          
          
          1 Reply Last reply Reply Quote 0
          • stackprogramer
            stackprogramer last edited by

            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);
            
            JonB 1 Reply Last reply Reply Quote 0
            • JonB
              JonB @stackprogramer last edited by

              @stackprogramer

              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....

              1 Reply Last reply Reply Quote 3
              • First post
                Last post