QFileSystemWatcher Problem
I had some problems when I used 'QFileSystemWatcher' to catch the QfileChanged signal. First I created my main class named A and then called a thread named B in A. Then I called a thread C in which I used QFileSystemWatcher to catch signal. But there is a problem that no signal emited. If I use QFileSystemWatcher in thread B, all going well. What' wrong? My OS is RHEL 6.1 Thanks for reply!!!
Code:
#include "threaduser.h"
#include "threadd.h"
#include "thread.h"
#include <qprocess.h>
#include <qthreadpool.h>
#include <QFile>
#include <qtextstream.h>
{
this->db=db;
}
void Threaduser::run()
{
//connect(&fs, SIGNAL(fileChanged(QString)), this, SLOT(changeread(QString)));
while(1)
{
pro->moveToThread(this);
pro->startDetached("./getwho.sh");
QFile f
("/tmp/who.txt");
//文件å //查看用户是å¦åˆšåˆšç™»å½•
while((name=out.readLine())!="")
{
name=t1[0];
address=t2[1].remove(")");
if(!newname.contains(name)||!newaddress.contains(address))
{
newname.append(name);
newaddress.append(address);
}
}
QStringList list1
=stillin
(namelist,newname
);
//ä¿æŒä»ç„¶åœ¨çº¿çš„进程 QStringList list2
=isout
(namelist,newname
);
//å·²ç»æŽ¨å‡ºåˆ—è¡¨ QStringList list3
=newin
(namelist,newname
);
//新增用户列表 QStringList list4
=stillin
(ipaddress,newaddress
);
//ä»ç„¶åœ¨çº¿ç”¨æˆ·IP地å€åˆ—表 QStringList list5
=isout
(ipaddress,newaddress
);
//å·²ç»æŽ¨å‡ºç”¨æˆ·åœ°å€åˆ—表 QStringList list6
=newin
(ipaddress,newaddress
);
//新增用户Ip地å€åˆ—表 for(int i=0;i<list3.length();i++)//产生新用户开辟新进程
{
//呿•°æ®åº“ä¸*写入用户登录
sql.prepare("INSERT INTO last (name,time,action,address) VALUES('"+list3[i]+"','"+time+"','login','"+list6[i]+"')");
sql.exec();
//å¼€å¯çº¿ç¨‹
Threadd* thread = new Threadd(db,list3[i]);
thread->start();//start();
//åŠ å…¥çº¿ç¨‹é˜Ÿåˆ—
m.append(thread);
namelist.append(list3[i]);
ipaddress.append(list6[i]);
/*QString path="/home/"+list3[i]+"/.bash_history";
fs.addPath(path);*/
}
for(int i=0;i<list2.length();i++)//å…³é—*退出用户的进程
{
//呿•°æ®åº“ä¸*写入用户退出
msl.prepare("INSERT INTO last (name,time,action,address) VALUES('"+list2[i]+"','"+outtime+"','logout','"+list5[i]+"')");
msl.exec();
//å…³é—*线程
for(int k=0;k<m.length();k++)
{
if(m[k]->name==list2[i])
{
m[k]->end();
m.removeAt(k);
}
}
//从类表ä¸*铲除他丫的
namelist.removeOne(list2[i]);
ipaddress.removeOne(list5[i]);
/*QString rmpath="/home/"+list2[i]+"/.bash_hisotry";
fs.removePath(rmpath);*/
}
sleep(1);
}
Thread* thread = new Thread();
thread->start();//start();
}
//ä»ç„¶åœ¨çº¿åˆ—表
{
for(int i=0;i<b.length();i++)
{
for(int j=0;j<a.length();j++)
{
if(b[i]==a[j])
{
ll.append(b[i]);
break;
}
}
}
return ll;
}
//刚刚退出列表
{
for(int i=0;i<b.length();i++)
{
a.removeOne(b[i]);
}
ll=a;
return ll;
}
//新近登陆列表
{
for(int i=0;i<a.length();i++)
{
b.removeOne(a[i]);
}
ll=b;
return ll;
}
//获å–当å‰ç³»ç»Ÿæ—¶é—´
{
QString now
= time.
toString("yyyy-MM-dd hh:mm:ss");
return now;
}
/*
void Threaduser::changeread(QString a)
{
QString path=a;
QStringList plist=path.split("/");
QString name=plist[2];
QFile f(a);
f.open(QFile::ReadOnly);
QTextStream out(&f);
//读å–åŽé¢ä¸¤è¡Œ
QString action;
while(1)
{
QString s=out.readLine();
if(s!="")
{
action=s;
}
else if(s=="")
{
break;
}
}
//写入数æ®åº“ä¸*
QString time=gettime();
QSqlQuery sql;
sql.prepare("INSERT INTO action (name, time ,action) VALUES('"+name+"','"+time+"','"+action+"')");
sql.exec();
}
*/
Code:
#include "threadd.h"
#include <qfilesystemwatcher.h>
#include <qtextstream.h>
#include <qfile.h>
//监控在线用户行为
{
this->name=name;
this->db=db;
}
void Threadd::run()
{
QString path
="D:/123.txt";
//"/home/"+name+"/.bash_history"; fs.addPath(path);
connect(&fs,
SIGNAL(fileChanged
(QString)),
this,
SLOT(changeread
(QString)));
}
void Threadd
::changeread(QString a
) {
//读å–åŽé¢ä¸¤è¡Œ
while(1)
{
if(s!="")
{
action=s;
}
else if(s=="")
{
break;
}
}
//写入数æ®åº“ä¸*
sql.prepare("INSERT INTO action (name, time ,action) VALUES('"+name+"','"+time+"','"+action+"')");
sql.exec();
}
//强制终æ*¢çº¿ç¨‹
void Threadd::end()
{
this->terminate();
}
{
QString now
= time.
toString("yyyy-MM-dd hh:mm:ss");
return now;
}
Re: QFileSystemWatcher Problem
Could you please share your code? It will give us much more informations on that.
Re: QFileSystemWatcher Problem
The file system watcher requires a running event loop.
Make sure the thread in which the watcher was created or to which it was moved runs its event loop.
Either by calling exec() in run() or not overwriting run() at all (base implementation runs an event loop).
Btw, some parts of your code do not make any sense.
For example you create a QProcess and then call a static method (startDetached), making the QProcess instance totally unneeded and useless.
Cheers,
_