Results 1 to 2 of 2

Thread: Need help with QFileSystemWatch

  1. #1
    Join Date
    Mar 2011
    Posts
    1
    Platforms
    Unix/X11

    Default Need help with QFileSystemWatch

    Hi, I need a little help:

    I got a file which gets from lirc new data all the time i press a key on my remote control. For example, I press the "1" key and the file contains "AN1", I press the "2" Key and the file gets overwritten with "AN2", and so on.

    My Qt program should permanenty read this file out and send the content to a server via socket. Well, at the time this works as follow ("datei" is the file I read out):

    Qt Code:
    1. void MainWindow::on_btnmonitor_clicked() // start file monitoring
    2. {
    3. {
    4. while(1){
    5. QFile file("datei"); // my file
    6. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    7. return;
    8.  
    9. while (!file.atEnd()) {
    10. QByteArray line = file.readLine();
    11.  
    12. if (line.contains("AUS1")){ //check if file contains "AUS1"
    13. char *buffer = "AUS1"; // send this to the buffer...
    14. write(sockfd, buffer, strlen(buffer)); // and write out to the server.
    15. }
    16. if (line.contains("AN1")){
    17. char *buffer = "AN1";
    18. write(sockfd, buffer, strlen(buffer));
    19. }
    20. usleep(30000);
    21. }
    22. }
    23. }
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, this code is horrible, not more than a general function test. The program is in an endless loop (but this doesn't matter, it has no other function) and my server gets flooded with messages all the time(that does matter). My goal is, that QtFileSystemwatcher monitors that file and only if there is a content-change the line should be sent to the server one time.

    But I have no idea, i read the tutorial of QtFilesystemWatcher, but got no idea how to catch this "file change" signal in an if-command and send only the "codeword" in this file to my server when it has changed. Or, is there a easier, better way instead of Filesystemwatch?

    Help, understandable for a newbie would be very nice;-)

    greets
    Last edited by seyanora; 29th March 2011 at 11:51.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help with QFileSystemWatch

    but got no idea how to catch this "file change" signal in an if-command and send only the "codeword" in this file to my server when it has changed.
    This means you don't know how signals and slot work.
    So you first have to read and understand how signals and slots work.
    http://doc.qt.nokia.com/latest/signalsandslots.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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.