PDA

View Full Version : pipe tail -f to Qlistbox realtime



unclecameron
10th December 2008, 01:40
I'm running a process with pyqt which produces a running output to /var/log/somefile.log. How can I pipe a realtime tail -f of somelogfile.log to a Qlistbox as the process is running, so far I have



f = os.system("tail -f /var/log/somefile.log")
while 1:
next = sys.stdin.readline(f)
self.resultsbox.insertItem(f)
if not next:
break


but nothing is displayed in my Qlistbox, what are best practices, is there another way I should be doing this?

caduel
10th December 2008, 07:37
You are not giving Qt a chance to repaint those changes.
The following approaches might work for you

Always: have an event loop running
i) use Qt signals/slots and read from tail via QProcess (this should work nicely; unless, perhaps, you are flooded with changes from "tail")
ii) setup a timer and poll "tail" actively when it timeout()s.
iii) keep your code but insert a call to QCoreApplication::processEvents().
(this will keep your app running in that loop. not nice.)

HTH

unclecameron
10th December 2008, 23:10
Okay, I've looked around and found a bit of code that make more sense, though it still doesn't work:


# set filename and open file
filename = '/var/log/somefile.log'
file = open(filename, 'r')
#Find file size and move to end
st_results = os.stat(filename)
st_size = st_results[6]
file.seek(st_size)
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
self.resultsbox.insertItem(file)


but I get an error:

QListBox.insertItem() has an invalid type

What do I need to do to my data to get QlistBox to accept it?

unclecameron
10th December 2008, 23:39
is there a simpler way to do all this with simply a console redirection to Qlistbox?