2

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

这是我的代码:

控制台.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

控制台.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();
}

主文件

#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();
}
4

1 回答 1

1

好的,解决了。我已经使用不同的线程尝试了 Yakk 的想法(感谢 Yakk 的想法)。我不得不引入一个新的QObject被调用子类MyObject。在它的构造函数中,我为控制台对象创建了Console一个新的。是在main.cppQThread中创建的,也是. 请参见下面的代码:QFileSystemWatcherMyObjcet

我的对象.h

#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

我的对象.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);
}

主文件

#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.h/.cpp没有被引用,只是Console::slotFileChanged(QString)被替换为Console::displayChangedFileText(QString).

于 2015-04-23T20:16:55.563 回答