QFileSystemWatcher不会在控制台应用程序中发出fileChanged()

时间:2015-04-22 20:07:09

标签: c++ qt console qfilesystemwatcher

在我的文件中 console.h / .cpp 我有一个小类,它只是要求用户键入一些文本,然后再次打印文本,直到用户输入“quit”(参见方法consoleMain())。但是,在 main.cpp 中我也有一个 QFileSystemWatcher监视文件 MyTextFile.txt ,并在文本文件发生更改时调用Console::slotFileChanged(QString)。不幸的是QFileSystemWatcher不起作用。更改文本文件时,永远不会执行Console::slotFileChanged(QString)。据我所知,QFileSystemWatcher只有在主事件循环已经启动时才有效,在我的代码中也是如此。 当我停用 main.cpp 中的QTimer::singlaShot并将其替换为emit console.signalStart()时 不会输入主事件循环,但在我输入“退出”后,我看到QFileSystemWatcher(“文件已更改!”)的消息。 问题是:当文本文件并行更改时,是否可以让用户与控制台交互并让FileWatcher发出信号? (我也尝试将QFileSystemWatcher放入控制台类并在堆上创建;不幸的是它没有改变任何东西)

这是我的代码:

console.h

#ifndef CONSOLE_H
#define CONSOLE_H

#include <iostream>
#include <QObject>
#include <QFileSystemWatcher>

class Console: public QObject
{
    Q_OBJECT

public:

    Console(QObject *parent = 0);
    ~Console();

signals:

    void signalStart();
    void signalEnd();

public slots:

    void consoleMain();
    void slotFileChanged(QString text);
    void slotEmit();
};

#endif // CONSOLE_H

console.cpp

#include "console.h"

Console::Console(QObject *parent): QObject(parent)
{
}

Console::~Console()
{
}

void Console::consoleMain()
{
    bool isRunning = true;
    std::string in;

    while (isRunning)
    {
        std::cout << ">" << std::flush;
        std::getline(std::cin, in);

        if (in.compare("quit") == 0)
            isRunning = false;
        else
            std::cout << "You have entered: " << in << std::endl;
    }

    emit signalEnd();
}

void Console::slotFileChanged(QString text)
{
    Q_UNUSED(text);
    std::cout << "File changed!" << std::endl;
}  

void Console::slotEmit()
{
    emit signalStart();
}

的main.cpp

#include "console.h"
#include <QCoreApplication>
#include <QTimer>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFileSystemWatcher watcher(&a);
    watcher.addPath("C:/MyTextFile.txt");

    Console console(&a);

    QObject::connect(&console, SIGNAL(signalStart()), &console, SLOT(consoleMain()));
    QObject::connect(&console, SIGNAL(signalEnd()), &a, SLOT(quit()));
    QObject::connect(&watcher, SIGNAL(fileChanged(QString)), &console, SLOT(slotFileChanged(QString)));

    QTimer::singleShot(0, &console, SLOT(slotEmit()));
    //emit console.signalStart();

    std::cout << "Enter main event loop now" << std::endl;
    return a.exec();
}

1 个答案:

答案 0 :(得分:1)

好的,它已经解决了。我尝试过使用不同线程的Yakk的想法(感谢Yakk的想法)。我不得不引入一个名为QObject的{​​{1}}的新子类。在它的构造函数中,我为控制台对象创建了MyObject和一个新的ConsoleQThread main.cpp 中创建,以及QFileSystemWatcher的实例。请参阅以下代码:

<强> myobject.h

MyObjcet

<强> myobject.cpp

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include "console.h"
#include <QThread>
#include <QCoreApplication>

class MyObject : public QObject
{
    Q_OBJECT

public:

    MyObject(QObject *parent = 0);
    ~MyObject();

private:

    QThread *thread;
    Console *console;

signals:

    void signalStart();

public slots:

    void slotFileChanged(QString text);
    void slotEnd();
};

#endif // MYOBJECT_H

<强>的main.cpp

#include "myobject.h"

MyObject::MyObject(QObject *parent): QObject(parent)
{
    console = new Console;

    thread = new QThread(this);
    console->moveToThread(thread);
    thread->start();

    connect(this, SIGNAL(signalStart()), console, SLOT(consoleMain()));
    connect(console, SIGNAL(signalEnd()), this, SLOT(slotEnd()));
    emit signalStart();
}

MyObject::~MyObject()
{
    thread->quit();
    thread->wait();
}

void MyObject::slotFileChanged(QString text)
{
    console->displayChangedFileText(text);
}

void MyObject::slotEnd()
{
    QCoreApplication::exit(0);
}

console.h / .cpp 没有制作,只有#include "myobject.h" #include <QTimer> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QFileSystemWatcher *watcher = new QFileSystemWatcher(&a); watcher->addPath("C:/MyTextFile.txt"); MyObject *object = new MyObject(&a); QObject::connect(watcher, SIGNAL(fileChanged(QString)), object, SLOT(slotFileChanged(QString))); std::cout << "Enter main event loop now" << std::endl; return a.exec(); } Console::slotFileChanged(QString)取代。