A minimal Qt C++ application that cyclically creates and deletes files in a folder while monitoring its contents using a state machine and threads.
- File Creation Thread: Cyclically creates files (
file_XXXX.txt) with minimal text content. - File Deletion Thread: Deletes the oldest file based on the consecutive number in the filename.
- State Machine: Monitors the directory and updates UI indicators:
- Green: 1–20 files.
- Red: 0 files.
- Orange: >20 files.
- UI:
- Button to choose the folder for file operations.
- Button to start/stop file creation/deletion.
- Button to select and open a file in the default text editor.
- Dashboard showing status (color indicator and file count) and a list of files.
- Spin boxes to adjust creation/deletion intervals (1000–10000 ms).
- Qt: Install Qt 5 or 6 (e.g., via Qt Online Installer).
- VS Code: Install Visual Studio Code.
- C++ Compiler: Ensure a C++ compiler (e.g., Clang) is installed (comes with Xcode on macOS).
- CMake or qmake: qmake is used by default (included with Qt).
src/:main.cpp: Application entry point.MainWindow.cpp/h: Main UI window with buttons, dashboard, and interval controls.FileCreator.cpp/h: Thread for cyclic file creation.FileDeleter.cpp/h: Thread for cyclic file deletion.FolderMonitor.cpp/h: State machine for monitoring folder contents.
FileMonitorApp.pro: Qt project file for qmake.README.md: This file.
- Choose Folder: Click "Choose Folder" to select a directory for file operations (defaults to
monitored_folderin the project directory). - Start/Stop Operations: Click "Start Operations" to begin file creation/deletion; click "Stop Operations" to pause.
- Open File: Click "Open File" to select and open a
.txtfile from the monitored folder in the default text editor. - Dashboard: Shows the status (color indicator and file count) and a list of files in the monitored folder.
- Adjust Intervals: Use the spin boxes to set creation/deletion intervals (1000–10000 ms). Default for Creation is 1000ms and Deletion is 5000ms
- The application uses QThread for file creation/deletion and QStateMachine for monitoring.
- Files are created as
file_XXXX.txt(e.g.,file_0001.txt) and deleted in order. - The folder must have write permissions for file operations to succeed.
-
Install Qt:
- Download and install Qt from www.qt.io.
- Choose a Qt version (e.g., Qt 5.15 or 6.x) and install qmake.
- Note the Qt installation path (e.g.,
/Users/youruser/Qt/5.15.2/clang_64).
-
Install VS Code:
- Download and install VS Code from code.visualstudio.com.
- Install the following extensions:
- C/C++ (by Microsoft)
- Qt Tools (optional, for Qt-specific features)
-
Set Up the Project:
- Clone or create the project directory (
FileMonitorApp) with the provided structure. - Ensure all files are placed as per the structure above.
- Clone or create the project directory (
-
Configure VS Code:
- Open the
FileMonitorAppfolder in VS Code. - Create a
tasks.jsonfile in.vscode/to build the project:{ "version": "2.0.0", "tasks": [ { "label": "Build with qmake", "type": "shell", "command": "/Users/youruser/Qt/5.15.2/clang_64/bin/qmake FileMonitorApp.pro && make", "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] }- Replace
/Users/youruser/Qt/5.15.2/clang_64/bin/qmakewith your Qt qmake path.
- Replace
- Create a
launch.jsonfile in.vscode/to run the application:{ "version": "0.2.0", "configurations": [ { "name": "Run FileMonitorApp", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/FileMonitorApp", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "lldb" } ] }
- Open the
-
Build the Project:
- Open the VS Code Command Palette (
Cmd+Shift+P). - Select
Tasks: Run Build Taskto run the qmake and make commands. - The binary
FileMonitorAppwill be generated in the project directory.
- Open the VS Code Command Palette (
-
Run the Application:
- Press
F5or selectRun > Start Debuggingin VS Code to launch the application. - Alternatively, run
./FileMonitorAppfrom the terminal in the project directory.
- Press