QFile ain't creating file when using QSerialPort
-
I am reading through serial port using signal and slots to connect to function whenever data is present, and inside this function I can't create a file to save data in it
code:
connect(serial, &QSerialPort::readyRead, this, &MainWindow::ReadPictureData); MainWindow::ReadPictureData(){ QFile ImageFile("./Image.JPG"); if(ImageFile.open(QIODevice::WriteOnly){ // Set code } }
-
@User94 said in QFile ain't creating file when using QSerialPort:
I can't create a file to save data in it
Why not? What goes wrong in the code you show?
If you use a relative path, as you do with
./Image.JPG
, you will not know where that is located. You should make an appropriate absolute path to the file you want to save to with the help of QStandardPaths Class.You will not want to create/open the save file in your
readyRead()
slot, as that will be called each time there is any serial data at all received, even a few bytes. You will want to create and open the file before or at the moment the first data is received, presumably storing theQFile
in a member variable which persists across multiplereadyRead()
signals. You will also need to know somehow when the complete file has been received so that you know when to close the file. -
When I use
./Image.JPG
file should be created in the output directory like it always does but it doesn't even when I specify a defined path... and when I use QFile::error it gives me this messageThe system cannot find the file specified.
I know when I create/open file insidereadyRead()
it will overwrite file each time a character is sent but I/m signaling here that when I use the signal and slot connect toQSerialPort
it is not working and in a simple project it is -
@User94 said in QFile ain't creating file when using QSerialPort:
When I use ./Image.JPG file should be created in the output directory
No, the file will be created in current working directory of your running application. Relative paths are relative to current working directory.
"I know when I create/open file inside readyRead() it will overwrite file" - you know that you can open file in append mode, so new content will be appended to the file? It will not be overwritten then?
-
@J-Hilk said in QFile ain't creating file when using QSerialPort:
QCoreApplication::applicationDirPath() + "/Image.JPG"
It's not the first time that I use QFile to create and write a file but it's the first time that I face this kind of problem it is supposed to be this simple and I told you in a simple project it is working fine I don't know what's causing this
-
@User94 said in QFile ain't creating file when using QSerialPort:
It's not the first time that I use QFile to create and write a file but it's the first time that I face this kind of problem it is supposed to be this simple and I told you in a simple project it is working fine I don't know what's causing this
Already told you how
QFile
treats a relative path. Perfectly simple, correct, and behaviour shared by other programming languages/libraries, not just Qt. If you regard this as "not simple" write to the developers of Linux/Windows and tell them you don't like how relative paths are dealt with in their OS.What is causing the issue is you not knowing what the current working directory of your running program is, or relying on it being something which is not guaranteed, e.g. run from Qt Creator will be different from run from command line or from desktop icon. If you use a relative path you are simply waiting for your code to behave differently/wrong in varying circumstances. And choosing
QDir::currentPath()
to make it absolute has no effect on the issue, it just makes it clearer what is wrong. -
Is strongly suspect a permission problem; try:
const QString imgPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); Q_ASSUME(QDir().mkpath(imgPath)); QFile ImageFile(imgPath + QLatin1String("/Image.JPG")); if(ImageFile.open(QIODevice::WriteOnly | QIODevice::Append){ // Set code }