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. Difference between write() and QFile.write()?
Forum Updated to NodeBB v4.3 + New Features

Difference between write() and QFile.write()?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 391 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.
  • A Offline
    A Offline
    akikaede
    wrote on last edited by
    #1

    Hi, I'm writing an application to display H.264 data stream through Fast-DDS, but there's something strange, files written by QFile cannot be played by smplayer, and file written by Linux write() played well. Also, the file written by QFile is not empty, it just cannot be played. I want to know what difference caused this. My Qt version is 5.18, here's my QFile code:

    QFile file( "/file/path" );
    if ( file.open(QIODevice::ReadOnly) )
    {
    file.write(buffer);  // buffer is a list of char pointer
    }
    

    There are problems when sending buffers, I use QBuffer to send buffers to mediaplayer, but it still cannot play in my mainwindow. I read buffers from a file to show, and it works well, but I want to play it from data stream. buffer in slot func is current data because I wrote it to a file, and it can be decoded. What is going wrong with my code?

    // signal
    QBuffer *buff = new QBuffer(this);
    buff->write(sample.message().data(), sample.message().size());
    emit dataAvaliable(buff);
    // slot
    buffer->open(QIODevice::ReadOnly);
    mediaPlayer->setMedia(QMediaContent(), buffer);
    mediaPlayer->play();
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • A akikaede

      Hi, I'm writing an application to display H.264 data stream through Fast-DDS, but there's something strange, files written by QFile cannot be played by smplayer, and file written by Linux write() played well. Also, the file written by QFile is not empty, it just cannot be played. I want to know what difference caused this. My Qt version is 5.18, here's my QFile code:

      QFile file( "/file/path" );
      if ( file.open(QIODevice::ReadOnly) )
      {
      file.write(buffer);  // buffer is a list of char pointer
      }
      

      There are problems when sending buffers, I use QBuffer to send buffers to mediaplayer, but it still cannot play in my mainwindow. I read buffers from a file to show, and it works well, but I want to play it from data stream. buffer in slot func is current data because I wrote it to a file, and it can be decoded. What is going wrong with my code?

      // signal
      QBuffer *buff = new QBuffer(this);
      buff->write(sample.message().data(), sample.message().size());
      emit dataAvaliable(buff);
      // slot
      buffer->open(QIODevice::ReadOnly);
      mediaPlayer->setMedia(QMediaContent(), buffer);
      mediaPlayer->play();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @akikaede said in Difference between write() and QFile.write()?:

      if ( file.open(QIODevice::ReadOnly) )
      {
      file.write(buffer); // buffer is a list of char pointer
      }

      This can't work: you open the file in read-only mode.

      Please read https://doc.qt.io/qt-6/qiodevice.html#write-1:
      "Writes data from a zero-terminated string of 8-bit characters to the device".
      Since you're writing binary data which can contain zero bytes you should use https://doc.qt.io/qt-6/qiodevice.html#write

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      3
      • jsulmJ jsulm

        @akikaede said in Difference between write() and QFile.write()?:

        if ( file.open(QIODevice::ReadOnly) )
        {
        file.write(buffer); // buffer is a list of char pointer
        }

        This can't work: you open the file in read-only mode.

        Please read https://doc.qt.io/qt-6/qiodevice.html#write-1:
        "Writes data from a zero-terminated string of 8-bit characters to the device".
        Since you're writing binary data which can contain zero bytes you should use https://doc.qt.io/qt-6/qiodevice.html#write

        A Offline
        A Offline
        akikaede
        wrote on last edited by
        #3

        @jsulm sorry for my typo, I use WriteOnly in my running program.

        1 Reply Last reply
        0
        • A akikaede

          Hi, I'm writing an application to display H.264 data stream through Fast-DDS, but there's something strange, files written by QFile cannot be played by smplayer, and file written by Linux write() played well. Also, the file written by QFile is not empty, it just cannot be played. I want to know what difference caused this. My Qt version is 5.18, here's my QFile code:

          QFile file( "/file/path" );
          if ( file.open(QIODevice::ReadOnly) )
          {
          file.write(buffer);  // buffer is a list of char pointer
          }
          

          There are problems when sending buffers, I use QBuffer to send buffers to mediaplayer, but it still cannot play in my mainwindow. I read buffers from a file to show, and it works well, but I want to play it from data stream. buffer in slot func is current data because I wrote it to a file, and it can be decoded. What is going wrong with my code?

          // signal
          QBuffer *buff = new QBuffer(this);
          buff->write(sample.message().data(), sample.message().size());
          emit dataAvaliable(buff);
          // slot
          buffer->open(QIODevice::ReadOnly);
          mediaPlayer->setMedia(QMediaContent(), buffer);
          mediaPlayer->play();
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @akikaede said in Difference between write() and QFile.write()?:

          files written by QFile cannot be played by smplayer, and file written by Linux write() played well

          There should be no difference. Compare the generated files byte-by-byte.

          1 Reply Last reply
          0

          • Login

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