Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How we can open a png file with QFile and then save it with new name......
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 749 Views 1 Watching
  • 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by stackprogramer
    #1

    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.HilkJ 1 Reply Last reply
    0
    • stackprogramerS 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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


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

      1 Reply Last reply
      5
      • stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by
        #3

        @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
        0
        • stackprogramerS Offline
          stackprogramerS Offline
          stackprogramer
          wrote on last edited by
          #4

          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);
          
          JonBJ 1 Reply Last reply
          0
          • stackprogramerS stackprogramer

            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);
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

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

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved