0

Explanations: I'm trying to create a daemon-script that will be watching the Watch folder and if I drop some files there - it should run the script.

Problem: sometimes after I drop the file - nothing happens

Script: the script is working with files in the Watch folder and deletes them after.

Additional question: do I have to delete mc object manually or it will be done automatically?

main.cpp

#include "widget.h"
#include <QFileSystemWatcher>
#include <QApplication>
#include <QDebug>

#include "myclass.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QFileSystemWatcher watcher;
    watcher.addPath("/home/user/Documents/Watch/");



    MyClass* mc = new MyClass;

    QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));



    return app.exec();
}

myclass.h

#ifndef MYCLASS
#define MYCLASS

#include <QWidget>
#include <QDir>
#include <QDebug>
#include <QProcess>

class MyClass : public QWidget
{
    Q_OBJECT

public:
    MyClass(QWidget* parent=0)
        :QWidget(parent){}


    ~MyClass(){}

public slots:
    void showModified(const QString& str)
    {

        if(QDir("/home/user/Documents/Watch/").entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).count() == 0)
        {
            qDebug()<<"EEEMPTYYYY!!!!!!!!!!!!!!!!!!!\n";
            _status = false;
        }

         Q_UNUSED(str)

        if (_status == false)
        {

         _status = true;

         QProcess *proc = new QProcess();
         proc->setWorkingDirectory("/home/user/Documents/");
         proc->startDetached("/home/user/Documents/script.sh");
         proc->waitForFinished();


         delete proc;


        }
    }

private:
    bool _status = false;
};


#endif // MYCLASS
user3027198
  • 183
  • 3
  • 11
  • The file watcher can be triggered multiple times by what _you_ think is a single change to the directory. I usually disable the slot for 500ms after each time it's called to prevent multiple occurrences. Your user isn't doing anything that fast. – Nicolas Holthaus Apr 24 '16 at 15:01
  • do I have to delete **mc** object manually or it will be done automatically? – user3027198 Apr 24 '16 at 15:06
  • 1
    Also, are you sure you can `waitForFinsihed` on a detached process? I think what you actually want to do is run the `QProcess` in another thread. Take a look at the [`runTestInThread` function](https://github.com/nholthaus/gtest-runner/blob/master/src/mainwindow_p.cpp) of gtest-runner. This program runs test executables every time they are modified on disk and is very similar to what you are trying to do. – Nicolas Holthaus Apr 24 '16 at 15:07
  • 1
    Any class you `new`, you have to `delete`. However, since the lifetime of the `mc` object is (seemingly) supposed to be the duration of the program, it doesn't really matter to this example. It's a bad habit though. Use a `std::unique_ptr` or `QScopedPointer` if you want to automatically manage the object lifetime. – Nicolas Holthaus Apr 24 '16 at 15:40
  • **Nicolas Holthaus,** so the memory will be managed automatically after the program closes? – user3027198 Apr 24 '16 at 18:19
  • 1
    When a program exits, the OS cleans up the memory space it was assigned, so yes, in a manner of speaking. – Nicolas Holthaus Apr 24 '16 at 19:31

0 Answers0