PDA

View Full Version : Need help with QFileSystemWatch



seyanora
29th March 2011, 11:44
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):


void MainWindow::on_btnmonitor_clicked() // start file monitoring
{
{
while(1){
QFile file("datei"); // my file
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

while (!file.atEnd()) {
QByteArray line = file.readLine();

if (line.contains("AUS1")){ //check if file contains "AUS1"
char *buffer = "AUS1"; // send this to the buffer...
write(sockfd, buffer, strlen(buffer)); // and write out to the server.
}
if (line.contains("AN1")){
char *buffer = "AN1";
write(sockfd, buffer, strlen(buffer));
}
usleep(30000);
}
}
}

}


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? :confused:

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

greets ;)

high_flyer
29th March 2011, 12:58
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