I want to write some simple PyQt torrent client, but I've got some snag with that. I would like to run some loop which takes care of downloading files (simple code using libtorrent) in a PyQt code. Although, when the torrent downloading works, the UI doesn't show up and when I change the order of called functions, the UI shows but downloading doesn't work. I read about QThreads but it's a bit hard for me - could anyone explain how does QThread work and how to use it with libtorrent? Here's some example of torrent client code:
import libtorrent as lt
import time
import sys
ses = lt.session()
ses.listen_on(6881, 6891)
info = lt.torrent_info(sys.argv[1])
h = ses.add_torrent({'ti': info, 'save_path': './'})
print 'starting', h.name()
while (not h.is_seed()):
s = h.status()
state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state]),
sys.stdout.flush()
time.sleep(1)
print h.name(), 'complete'
I would imagine QT runs a message loop that doesn't return until the application is terminated.
What I believe you want to do is to move the loop body (that polls the libtorrent status) into a message handler that's called regularly by QT. Say, a timer function.
Firstly I would recommend the PySide implementation of Qt.
In that situation (and plain pyQt) you should look into signals and slots:
http://qt-project.org/wiki/Signals_and_Slots_in_PySide
http://zetcode.com/gui/pyqt4/eventsandsignals/
Basically you just need to set up what is usually called a 'Worker' class that is just a QThread.
class Worker(QtCore.QThread):
updateProgress = QtCore.Signal(int) #or whatever you wanna call it
This ^^^ is where the code above goes. Then what you want to do is connect the signal in the main GUI thread / class.
self.worker.updateProgress.connect(self.setProgress) #notice no ()
def setProgress(self, progress):
self.progressBar.setValue(progress)
Then finally inside the function that will output values you want:
self.updateProgress.emit(value)
This example implies something like a progress bar functionality. I know this post is old -- but this would have helped me so hopefully the googlers will find it useful ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.