我正在跟踪另一个应用程序更改的日志文件。在linux中,我会在其他应用程序更改文件后立即正确接收fileChanged信号。在Windows中,QFileSystemWatcher在其他应用程序关闭之前不会发出任何fileChanged信号。
我试图用记事本打开日志以确保实际上已被更改,并且只要记事本打开日志,QFileSystemWatcher就会发送fileChanged信号。
我的代码:
void LogLoader::createFileWatcher()
{
if(fileWatcher != NULL) delete fileWatcher;
fileWatcher = new QFileSystemWatcher(this);
connect(fileWatcher, SIGNAL(fileChanged(QString)),
this, SLOT(prepareLogWorker(QString)));
if(fileWatcher->addPath(logPath))
{
qDebug() << "LogLoader: "<< "FileWatcher linked.";
}
}
void LogLoader::prepareLogWorker(QString path)
{
//Added this just in case because I read it as solution
//in other question. But in my case the file is not removed.
if (!fileWatcher->files().contains(path))
{
fileWatcher->addPath(path);
}
QTimer::singleShot(1000, this, SLOT(sendLogWorker()));
}
我做错了吗?除了检查之外还有其他解决方案 文件不时手动?