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. QFile ain't creating file when using QSerialPort
Forum Updated to NodeBB v4.3 + New Features

QFile ain't creating file when using QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 696 Views
  • 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.
  • U Offline
    U Offline
    User94
    wrote on last edited by
    #1

    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
           }
    }
    JonBJ 1 Reply Last reply
    0
    • U User94

      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
             }
      }
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 the QFile in a member variable which persists across multiple readyRead() signals. You will also need to know somehow when the complete file has been received so that you know when to close the file.

      1 Reply Last reply
      1
      • U Offline
        U Offline
        User94
        wrote on last edited by
        #3

        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 message The system cannot find the file specified.
        I know when I create/open file inside readyRead() it will overwrite file each time a character is sent but I/m signaling here that when I use the signal and slot connect to QSerialPort it is not working and in a simple project it is

        jsulmJ 1 Reply Last reply
        0
        • U User94

          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 message The system cannot find the file specified.
          I know when I create/open file inside readyRead() it will overwrite file each time a character is sent but I/m signaling here that when I use the signal and slot connect to QSerialPort it is not working and in a simple project it is

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          2
          • U Offline
            U Offline
            User94
            wrote on last edited by
            #5

            @jsulm I replaced QFile ImageFile("./Image.JPG") with QFile thisFile(QDir::currentPath() + "./Image.JPG"); and still getting same error

            J.HilkJ 1 Reply Last reply
            0
            • U User94

              @jsulm I replaced QFile ImageFile("./Image.JPG") with QFile thisFile(QDir::currentPath() + "./Image.JPG"); and still getting same error

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @User94

              QCoreApplication::applicationDirPath() + "/Image.JPG"


              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
              0
              • U Offline
                U Offline
                User94
                wrote on last edited by
                #7

                @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

                JonBJ 1 Reply Last reply
                0
                • U User94

                  @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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

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

                  1 Reply Last reply
                  2
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    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
                    }

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    2

                    • Login

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