Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. File not found

File not found

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlfiledirectoryfile not found
5 Posts 2 Posters 1.1k 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.
  • T Offline
    T Offline
    texasRanger
    wrote on last edited by texasRanger
    #1

    hello,

    I have a program that reads data from a json file and displays that data onto a display screen. I'm using QFileSystemWatcher so if there is a change in the file, then with signal/slot it will update the display with the correct new data. When i first run the code it opens the json file, pulls the data and displays it just fine. However, when i make a change to the data within the file, the signal gets sent and runs the code that pulls the new data from the file, except the file says it can't be found. So simply, the file opens the first time around, but can't be located any time after that. The file name nor the path changes.

    //readJSON.cpp
    
    QString readJSON::readingJson(const QString &name)
    {
        QFile file;
    
        file.setFileName("C:/Users/RnThs/Desktop/Projects/data.json");
    
        if(!file.exists()){
            qDebug()<<"File does not exist";
            exit(1);
        }
    
        if(!file.open(QIODevice::ReadOnly)){
            qDebug()<<"Failed to open";
            exit(1);
        }
    
        QTextStream file_text(&file);
        QString json_string;
        json_string = file_text.readAll();
        file.close();
    
        QByteArray json_bytes = json_string.toLocal8Bit();
        auto json_doc=QJsonDocument::fromJson(json_bytes);
    
        QJsonObject json_obj=json_doc.object();
    
        QVariantMap json_map = json_obj.toVariantMap();
    
        return json_map[name].toString();
    }
    
    JonBJ 1 Reply Last reply
    0
    • T texasRanger

      hello,

      I have a program that reads data from a json file and displays that data onto a display screen. I'm using QFileSystemWatcher so if there is a change in the file, then with signal/slot it will update the display with the correct new data. When i first run the code it opens the json file, pulls the data and displays it just fine. However, when i make a change to the data within the file, the signal gets sent and runs the code that pulls the new data from the file, except the file says it can't be found. So simply, the file opens the first time around, but can't be located any time after that. The file name nor the path changes.

      //readJSON.cpp
      
      QString readJSON::readingJson(const QString &name)
      {
          QFile file;
      
          file.setFileName("C:/Users/RnThs/Desktop/Projects/data.json");
      
          if(!file.exists()){
              qDebug()<<"File does not exist";
              exit(1);
          }
      
          if(!file.open(QIODevice::ReadOnly)){
              qDebug()<<"Failed to open";
              exit(1);
          }
      
          QTextStream file_text(&file);
          QString json_string;
          json_string = file_text.readAll();
          file.close();
      
          QByteArray json_bytes = json_string.toLocal8Bit();
          auto json_doc=QJsonDocument::fromJson(json_bytes);
      
          QJsonObject json_obj=json_doc.object();
      
          QVariantMap json_map = json_obj.toVariantMap();
      
          return json_map[name].toString();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @texasRanger said in File not found:

      However, when i make a change to the data within the file

      How do you do that? Some external tool? My guess is that tool is keeping the file open so you cannot access it again? Or even renaming it...? Are you Windows?

      except the file says it can't be found.

      So does it hit your "File does not exist" or "Failed to open", you don't say which?

      T 1 Reply Last reply
      1
      • JonBJ JonB

        @texasRanger said in File not found:

        However, when i make a change to the data within the file

        How do you do that? Some external tool? My guess is that tool is keeping the file open so you cannot access it again? Or even renaming it...? Are you Windows?

        except the file says it can't be found.

        So does it hit your "File does not exist" or "Failed to open", you don't say which?

        T Offline
        T Offline
        texasRanger
        wrote on last edited by
        #3

        @JonB yes, windows. It hits the first check "File does not exist". i manually went out to the file, made a change, and then closed the file.

        JonBJ 1 Reply Last reply
        0
        • T texasRanger

          @JonB yes, windows. It hits the first check "File does not exist". i manually went out to the file, made a change, and then closed the file.

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

          @texasRanger said in File not found:

          i manually went out to the file, made a change, and then closed the file.

          Can't you just say how? If it was in, say, Notepad, why not say, "I did it in Notepad, and exited"?

          Anyway that was my thought.

          From the code you show you seem to be closing the file properly, might be good to put in a qDebug() to make sure. Are you sure you don't do something else to the file, like delete or rename it? Get rid of your QFileSystemWatcher and see? Is it possible to have a race condition between QFileSystemWatcher doing something and you try the exists() just too early? Put in a delay to determine.

          T 1 Reply Last reply
          1
          • JonBJ JonB

            @texasRanger said in File not found:

            i manually went out to the file, made a change, and then closed the file.

            Can't you just say how? If it was in, say, Notepad, why not say, "I did it in Notepad, and exited"?

            Anyway that was my thought.

            From the code you show you seem to be closing the file properly, might be good to put in a qDebug() to make sure. Are you sure you don't do something else to the file, like delete or rename it? Get rid of your QFileSystemWatcher and see? Is it possible to have a race condition between QFileSystemWatcher doing something and you try the exists() just too early? Put in a delay to determine.

            T Offline
            T Offline
            texasRanger
            wrote on last edited by
            #5

            @JonB Oh yeah sorry.. I wasn't reading the right. It was opening in a VS text editor. I tried running it with notepad and it worked. I guess VS has something where they keep the file open, so i couldn't edit it and then update the display.

            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