Results 1 to 4 of 4

Thread: pipe tail -f to Qlistbox realtime

  1. #1
    Join Date
    Dec 2008
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default pipe tail -f to Qlistbox realtime

    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

    Qt Code:
    1. f = os.system("tail -f /var/log/somefile.log")
    2. while 1:
    3. next = sys.stdin.readline(f)
    4. self.resultsbox.insertItem(f)
    5. if not next:
    6. break
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pipe tail -f to Qlistbox realtime

    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

  3. #3
    Join Date
    Dec 2008
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: pipe tail -f to Qlistbox realtime

    Okay, I've looked around and found a bit of code that make more sense, though it still doesn't work:
    Qt Code:
    1. # set filename and open file
    2. filename = '/var/log/somefile.log'
    3. file = open(filename, 'r')
    4. #Find file size and move to end
    5. st_results = os.stat(filename)
    6. st_size = st_results[6]
    7. file.seek(st_size)
    8. while 1:
    9. where = file.tell()
    10. line = file.readline()
    11. if not line:
    12. time.sleep(1)
    13. file.seek(where)
    14. else:
    15. self.resultsbox.insertItem(file)
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by unclecameron; 10th December 2008 at 23:37.

  4. #4
    Join Date
    Dec 2008
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: pipe tail -f to Qlistbox realtime

    is there a simpler way to do all this with simply a console redirection to Qlistbox?

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.