2013-05-21 2 views
0

몇 가지 간단한 PyQt 토런트 클라이언트를 작성하고 싶지만 그걸로 몇 가지 걸림돌이 있습니다. PyQt 코드에서 파일 다운로드 (libtorrent를 사용하는 간단한 코드)를 처리하는 루프를 실행하고 싶습니다. 토런트 다운로드가 작동하면 UI가 표시되지 않지만 호출 된 함수의 순서를 변경하면 UI가 표시되지만 다운로드가 작동하지 않습니다. QThreads에 대해 읽었지 만 나에게 조금 어렵다. 누구나 QThread의 작동 방식과 libtorrent와 함께 사용하는 방법을 설명 할 수 있습니까? 내가 상상QThreads를 PyQt에서 libtorrent와 함께 사용하는 방법은 무엇입니까?

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' 

답변

1

QT 응용 프로그램이 종료 될 때까지 반환하지 않는 메시지 루프를 실행합니다 여기 토런트 클라이언트 코드의 몇 가지 예입니다.

내가 원하는 것은 QT에서 정기적으로 호출하는 메시지 처리기로 libtorrent 상태를 폴링하는 루프 본문을 이동하는 것입니다. 타이머 기능을 말해봐.

+0

그게 내 목적입니다. –

1

먼저 PySide 구현을 권장합니다. 기본적으로 당신은 그냥 일반적으로 '노동자'클래스라는 것을 설정해야

http://zetcode.com/gui/pyqt4/eventsandsignals/

http://qt-project.org/wiki/Signals_and_Slots_in_PySide : 그 상황 (및 일반 PyQt는) 당신은 신호와 슬롯에 보일 것입니다에서

그것은 단지 QThread입니다.

class Worker(QtCore.QThread): 
    updateProgress = QtCore.Signal(int) #or whatever you wanna call it 

위의 코드는 어디까지입니까? 그런 다음 주 GUI 스레드/클래스에 신호를 연결하십시오.

self.worker.updateProgress.connect(self.setProgress) #notice no() 


def setProgress(self, progress): 
    self.progressBar.setValue(progress) 

그런 다음 마지막으로 출력 값이 원하는 것 기능 내부 :

self.updateProgress.emit(value) 

이 예제는 진행률 표시 줄 기능 같은 것을 의미한다. 나는이 게시물이 낡았다는 것을 알고 있습니다. 그러나 이것은 내게 도움이되었으므로 유용하다고 생각할 것입니다.)