QThread의 method를 QThread의 이벤트루프에서 실행되게 하는 방법

Code Genie·2025년 1월 8일

class EventHookThread(QThread):
    setHwndSignal = Signal(int)
    
    def __init__(self):
        super().__init__()
        self.setHwndSignal.connect(self.set_hwnd)
        self.moveToThread(self) # 이 코드 중요
        
    def run(self):
        threading.current_thread().name = f"EventHookThread {threading.currentThread().native_id}"
        logger.warning(f"run thread: {threading.currentThread().native_id} {threading.current_thread().name}")
        self.exec() # QThread의 이벤트 루프 실행
        
    @Slot(int)
    def set_hwnd(self, hwnd):
        logger.warning(f"set_hwnd: {threading.currentThread().native_id} {threading.current_thread().name}")
        logger.warning(f"set_hwnd {hwnd}")
        if hwnd is None:
            return


# 메인스레드 부분 =================
방법1. event_hook_thread.setHwndSignal.emit(hwnd)
방법2. QMetaObject.invokeMethod(event_hook_thread, "set_hwnd", Qt.QueuedConnection, Q_ARG(int, hwnd))

Q_ARG(int, hwnd) => 이 코드가 중요
profile
1인 개발

0개의 댓글