PDA

View Full Version : QFileSystemWatcher Problem



Dongxd
10th May 2014, 06:59
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!!!


#include "threaduser.h"
#include "threadd.h"
#include "thread.h"
#include <qprocess.h>
#include <qthreadpool.h>
#include <QFile>
#include <qtextstream.h>

Threaduser::Threaduser(QSqlDatabase db,QObject *parent) :
QThread(parent)
{
this->db=db;
}

void Threaduser::run()
{
//connect(&fs, SIGNAL(fileChanged(QString)), this, SLOT(changeread(QString)));
while(1)
{
QProcess* pro=new QProcess();
pro->moveToThread(this);
pro->startDetached("./getwho.sh");
QFile f("/tmp/who.txt");//文件名
f.open(QFile::ReadOnly);
QTextStream out(&f);
QString name,address;
QStringList newname;
QStringList newaddress;
//查看用户是否刚刚 登录
while((name=out.readLine())!="")
{
QStringList t1=name.split(" ");
QStringList t2=name.split("(");
name=t1[0];
address=t2[1].remove(")");
if(!newname.contains(name)||!newaddress.contains(a ddress))
{
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++)//äº§ç”Ÿæ–°ç”¨æˆ·å¼€è¾Ÿæ °è¿›ç¨‹
{
//向数据库ä¸*å†™å…¥ç ”¨æˆ·ç™»å½•
QString time = gettime();
QSqlQuery sql;
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++)//å…³é—*退出用户的 ¿›ç¨‹
{
//向数据库ä¸*å†™å…¥ç ”¨æˆ·é€€å‡º
QString outtime = gettime();
QSqlQuery msl;
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();
}

//仍然在线列表
QStringList Threaduser::stillin(QStringList a,QStringList b)
{
QStringList ll;
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;
}

//刚刚退出列表
QStringList Threaduser::isout(QStringList a,QStringList b)
{
QStringList ll;
for(int i=0;i<b.length();i++)
{
a.removeOne(b[i]);
}
ll=a;
return ll;
}

//新近登陆列表
QStringList Threaduser::newin(QStringList a,QStringList b)
{
QStringList ll;
for(int i=0;i<a.length();i++)
{
b.removeOne(a[i]);
}
ll=b;
return ll;
}

//èŽ·å–å½“å‰ç³»ç»Ÿæ—¶é ´
QString Threaduser::gettime()
{
QDateTime time = QDateTime::currentDateTime();
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();

}
*/




#include "threadd.h"
#include <qfilesystemwatcher.h>
#include <qtextstream.h>
#include <qfile.h>
//监控在线用户行ä¸

Threadd::Threadd(QSqlDatabase db,QString name,QObject *parent) :
QThread(parent)
{
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)
{
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();
}

//强制终æ*¢çº¿ç¨‹
void Threadd::end()
{
this->terminate();
}

QString Threadd::gettime()
{
QDateTime time = QDateTime::currentDateTime();
QString now = time.toString("yyyy-MM-dd hh:mm:ss");
return now;
}

sonulohani
10th May 2014, 09:57
Could you please share your code? It will give us much more informations on that.

anda_skoa
10th May 2014, 11:18
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,
_