Difference between write() and QFile.write()?
-
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();
-
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();
@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 -
@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 -
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();