在我的文件console.h/.cpp中,我有一个小类,它只要求用户键入一些文本,然后再次打印文本,直到用户输入"退出"(请参阅方法consoleMain()
)。但是,在main.cpp中,我也有一个QFileSystemWatcher
监视文件MyTextFile.txt,并在文本文件更改时调用Console::slotFileChanged(QString)
。不幸的是,QFileSystemWatcher
无法工作。当我更改文本文件时,Console::slotFileChanged(QString)
从不执行。据我所知,QFileSystemWatcher
只有在主事件循环已经启动的情况下才能工作,在我的代码中也是如此。当我禁用main.cpp中的QTimer::singlaShot
并将其替换为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
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();
}
好的,解决了。我用不同的线程尝试过亚克的想法(感谢亚克的创意)。我不得不引入一个新的QObject
子类,称为MyObject
。在它的构造函数中,我为控制台对象创建了Console
和一个新的QThread
。QFileSystemWatcher
是在main.cpp中创建的,也是MyObjcet
的一个实例。参见以下代码:
myobject.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
myobject.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);
}
main.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.h/.cpp未被管理,只有Console::slotFileChanged(QString)
被Console::displayChangedFileText(QString)
取代。