Results 1 to 3 of 3

Thread: QFileSystemWatcher Problem

  1. #1
    Join Date
    May 2014
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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!!!
    Qt Code:
    1. #include "threaduser.h"
    2. #include "threadd.h"
    3. #include "thread.h"
    4. #include <qprocess.h>
    5. #include <qthreadpool.h>
    6. #include <QFile>
    7. #include <qtextstream.h>
    8.  
    9. Threaduser::Threaduser(QSqlDatabase db,QObject *parent) :
    10. QThread(parent)
    11. {
    12. this->db=db;
    13. }
    14.  
    15. void Threaduser::run()
    16. {
    17. //connect(&fs, SIGNAL(fileChanged(QString)), this, SLOT(changeread(QString)));
    18. while(1)
    19. {
    20. QProcess* pro=new QProcess();
    21. pro->moveToThread(this);
    22. pro->startDetached("./getwho.sh");
    23. QFile f("/tmp/who.txt");//文件名
    24. f.open(QFile::ReadOnly);
    25. QTextStream out(&f);
    26. QString name,address;
    27. QStringList newname;
    28. QStringList newaddress;
    29. //查看用户是否刚刚登录
    30. while((name=out.readLine())!="")
    31. {
    32. QStringList t1=name.split(" ");
    33. QStringList t2=name.split("(");
    34. name=t1[0];
    35. address=t2[1].remove(")");
    36. if(!newname.contains(name)||!newaddress.contains(address))
    37. {
    38. newname.append(name);
    39. newaddress.append(address);
    40. }
    41. }
    42. QStringList list1=stillin(namelist,newname);//保持仍然在线的进程
    43. QStringList list2=isout(namelist,newname);//已经推出列表
    44. QStringList list3=newin(namelist,newname);//新增用户列表
    45. QStringList list4=stillin(ipaddress,newaddress);//仍然在线用户IP地址列表
    46. QStringList list5=isout(ipaddress,newaddress);//已经推出用户地址列表
    47. QStringList list6=newin(ipaddress,newaddress);//新增用户Ip地址列表
    48. for(int i=0;i<list3.length();i++)//产生新用户开辟新进程
    49. {
    50. //向数据库ä¸*写入用户登录
    51. QString time = gettime();
    52. QSqlQuery sql;
    53. sql.prepare("INSERT INTO last (name,time,action,address) VALUES('"+list3[i]+"','"+time+"','login','"+list6[i]+"')");
    54. sql.exec();
    55. //开启线程
    56. Threadd* thread = new Threadd(db,list3[i]);
    57. thread->start();//start();
    58. //åŠ å…¥çº¿ç¨‹é˜Ÿåˆ—
    59. m.append(thread);
    60. namelist.append(list3[i]);
    61. ipaddress.append(list6[i]);
    62. /*QString path="/home/"+list3[i]+"/.bash_history";
    63.   fs.addPath(path);*/
    64. }
    65. for(int i=0;i<list2.length();i++)//å…³é—*退出用户的进程
    66. {
    67. //向数据库ä¸*写入用户退出
    68. QString outtime = gettime();
    69. QSqlQuery msl;
    70. msl.prepare("INSERT INTO last (name,time,action,address) VALUES('"+list2[i]+"','"+outtime+"','logout','"+list5[i]+"')");
    71. msl.exec();
    72. //å…³é—*线程
    73. for(int k=0;k<m.length();k++)
    74. {
    75. if(m[k]->name==list2[i])
    76. {
    77. m[k]->end();
    78. m.removeAt(k);
    79. }
    80. }
    81. //从类表ä¸*铲除他丫的
    82. namelist.removeOne(list2[i]);
    83. ipaddress.removeOne(list5[i]);
    84. /*QString rmpath="/home/"+list2[i]+"/.bash_hisotry";
    85.   fs.removePath(rmpath);*/
    86. }
    87. sleep(1);
    88. }
    89. Thread* thread = new Thread();
    90. thread->start();//start();
    91. }
    92.  
    93. //仍然在线列表
    94. QStringList Threaduser::stillin(QStringList a,QStringList b)
    95. {
    96. for(int i=0;i<b.length();i++)
    97. {
    98. for(int j=0;j<a.length();j++)
    99. {
    100. if(b[i]==a[j])
    101. {
    102. ll.append(b[i]);
    103. break;
    104. }
    105. }
    106. }
    107. return ll;
    108. }
    109.  
    110. //刚刚退出列表
    111. QStringList Threaduser::isout(QStringList a,QStringList b)
    112. {
    113. for(int i=0;i<b.length();i++)
    114. {
    115. a.removeOne(b[i]);
    116. }
    117. ll=a;
    118. return ll;
    119. }
    120.  
    121. //新近登陆列表
    122. QStringList Threaduser::newin(QStringList a,QStringList b)
    123. {
    124. for(int i=0;i<a.length();i++)
    125. {
    126. b.removeOne(a[i]);
    127. }
    128. ll=b;
    129. return ll;
    130. }
    131.  
    132. //获取当前系统时间
    133. QString Threaduser::gettime()
    134. {
    135. QDateTime time = QDateTime::currentDateTime();
    136. QString now = time.toString("yyyy-MM-dd hh:mm:ss");
    137. return now;
    138. }
    139. /*
    140. void Threaduser::changeread(QString a)
    141. {
    142.   QString path=a;
    143.   QStringList plist=path.split("/");
    144.   QString name=plist[2];
    145.   QFile f(a);
    146.   f.open(QFile::ReadOnly);
    147.   QTextStream out(&f);
    148.   //读取后面两行
    149.   QString action;
    150.   while(1)
    151.   {
    152.   QString s=out.readLine();
    153.   if(s!="")
    154.   {
    155.   action=s;
    156.   }
    157.   else if(s=="")
    158.   {
    159.   break;
    160.   }
    161.   }
    162.   //写入数据库ä¸*
    163.   QString time=gettime();
    164.   QSqlQuery sql;
    165.   sql.prepare("INSERT INTO action (name, time ,action) VALUES('"+name+"','"+time+"','"+action+"')");
    166.   sql.exec();
    167.  
    168. }
    169. */
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "threadd.h"
    2. #include <qfilesystemwatcher.h>
    3. #include <qtextstream.h>
    4. #include <qfile.h>
    5. //监控在线用户行为
    6.  
    7. Threadd::Threadd(QSqlDatabase db,QString name,QObject *parent) :
    8. QThread(parent)
    9. {
    10. this->name=name;
    11. this->db=db;
    12. }
    13.  
    14. void Threadd::run()
    15. {
    16. QString path="D:/123.txt";//"/home/"+name+"/.bash_history";
    17. fs.addPath(path);
    18. connect(&fs, SIGNAL(fileChanged(QString)), this, SLOT(changeread(QString)));
    19. }
    20.  
    21. void Threadd::changeread(QString a)
    22. {
    23. QFile f(a);
    24. f.open(QFile::ReadOnly);
    25. QTextStream out(&f);
    26. //读取后面两行
    27. QString action;
    28. while(1)
    29. {
    30. QString s=out.readLine();
    31. if(s!="")
    32. {
    33. action=s;
    34. }
    35. else if(s=="")
    36. {
    37. break;
    38. }
    39. }
    40. //写入数据库ä¸*
    41. QString time=gettime();
    42. QSqlQuery sql;
    43. sql.prepare("INSERT INTO action (name, time ,action) VALUES('"+name+"','"+time+"','"+action+"')");
    44. sql.exec();
    45. }
    46.  
    47. //强制终æ*¢çº¿ç¨‹
    48. void Threadd::end()
    49. {
    50. this->terminate();
    51. }
    52.  
    53. QString Threadd::gettime()
    54. {
    55. QDateTime time = QDateTime::currentDateTime();
    56. QString now = time.toString("yyyy-MM-dd hh:mm:ss");
    57. return now;
    58. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Dongxd; 10th May 2014 at 10:05.

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemWatcher Problem

    Could you please share your code? It will give us much more informations on that.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    Dongxd (10th May 2014)

Similar Threads

  1. Problem with QFileSystemWatcher?
    By arbi in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2012, 07:57
  2. QFileSystemWatcher problem... yes, again..
    By nateriver in forum Qt Programming
    Replies: 0
    Last Post: 31st December 2009, 16:47
  3. QFIleSystemWatcher
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 08:21
  4. QFileSystemWatcher problem!!
    By Raajesh in forum Qt Programming
    Replies: 0
    Last Post: 23rd June 2008, 17:46
  5. Need help with QFileSystemWatcher
    By L.Marvell in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2006, 13:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.