pyqt5 이벤트 이벤트 필터 install EventFilter 이벤트 QEvent.MetaCall

15994 단어 pyqt
코드:
from PyQt5.QtCore import QThread, pyqtSignal, QDateTime, QObject,QEvent
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QLabel
import time
import sys


class BackendThread(QObject):
    #            
    update_date = pyqtSignal(str)

    #       
    def run(self):
        while True:
            data = QDateTime.currentDateTime()
            currTime = data.toString("yyyy-MM-dd hh:mm:ss")
            self.update_date.emit(str(currTime))
            time.sleep(1)


class Window(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setWindowTitle('PyQt 5        ')
        self.resize(400, 100)
        self.input = QLabel(self)
        self.input.resize(400, 100)
        self.initUI()

    def initUI(self):
        #     
        self.backend = BackendThread()
        #     
        self.backend.update_date.connect(self.handleDisplay)
        self.thread = QThread()
        self.backend.moveToThread(self.thread)
        #     
        self.thread.started.connect(self.backend.run)
        self.thread.start()



    #            
    def handleDisplay(self, data):
        self.input.setText(data)

    def keyPressEvent(self, event):
        print('keyPressEvent')
        pass

    def eventFilter(self, objwatched, event):
        eventType = event.type()
        #print(eventType)
        if eventType == QEvent.KeyPress:
            print('eventFilter KeyPress')

        elif eventType == QEvent.MetaCall:
            print('eventFilter MetaCall')
        return super().eventFilter(objwatched, event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    app.installEventFilter(win)
    sys.exit(app.exec_())

참조 자료:


PyQt 학습 에세이: Qt 이벤트 QEvent.type 유형 상량 및 의미 자료 요약 상세 내용 속찰https://blog.csdn.net/LaoYuanPython/article/details/102527651
PyQt5 - 이벤트 처리 메커니즘https://www.cnblogs.com/ygzhaof/p/10132824.html
 def eventFilter(self, obj, event):
       
       if obj == self.lineEdit:
        if event.type()== QEvent.FocusIn:
            #self.inp_text_signal.emit("  ")
            if self.lineEdit.text().strip() == '      ':
                self.lineEdit.clear()
            
                
            print("ok1")
        elif event.type()== QEvent.FocusOut:
            if self.lineEdit.text().strip() == '':
                self.lineEdit.setText("      ")
            print("ok2")
        else:
            pass
        return False
        

에서 소개한 Qt가 정의한 이벤트 외에 Qt는 사용자 정의 이벤트도 지원한다.
편의를 위해 register EventType () 함수를 사용하여 프로그램에 사용자 정의 이벤트 형식을 등록하고 보존할 수 있습니다.이렇게 하면 프로그램에서 다른 곳에서 사용되고 있는 사용자 정의 이벤트 형식을 의외로 다시 사용하는 것을 피할 수 있다.
사용자 정의 이벤트의 값은 QEvent.에 있어야 합니다.User 이벤트 - QEvent.MaxUser 이벤트 사이, 여기서 QEvent.User 이벤트의 상수 값은 1000, QEvent입니다.MaxUser 이벤트의 상수 값은 65535입니다.PyQt 학습 에세이: 설치 이벤트Filter를 통해 다시 쓰는 이벤트Filter를 설치하여 응용 이벤트를 포착하는 방법https://blog.csdn.net/LaoYuanPython/article/details/1025397738.registerEventType(hint=-1) 방법은 사용자 정의 이벤트 형식을 등록하고 되돌려주는 데 사용되며 이것은 루트가 안전한 함수입니다.그 중

참조:


PyQt5 윈도우즈 시스템 메시지 인식 장치를 사용하여 플러그인
키워드:python,PyQt5,nativeEvent,WMDEVICECHANGE, winEvent
다음 코드는 이미 간소화되어 무관한 부분을 제외하고 직접 실행할 수 있으며 실행 환경은python3.7.2 64bit PyQt5.11.3 64bit
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from ctypes.wintypes import MSG

class MainWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        
    def nativeEvent(self, eventType, msg):
        message = MSG.from_address(int(msg))
        if message.message == 0x219:  # WM_DEVICECHANGE
            if message.wParam == 0x8000:  # DBT_DEVICEARRIVAL
                print("in")
            elif message.wParam == 0x8004:  # DBT_DEVICEREMOVECOMPLETE
                print("out")
            elif message.wParam == 0x0007:  # DBT_DEVNODE_CHANGED
                print("node")
        return False, msg

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

운행할 때 아주 작은 인터페이스가 하나 있는데 인터페이스 안에 아무것도 없다.플러그 장치는 해석기의 단말기에서 출력됩니다.
이전부터 PyQt5로 윈도우즈 시스템 메시지를 식별하고 싶었다.근데 오늘부터 이거 하러 갔어.많은 자료를 뒤졌는데 반나절이 걸려서야 겨우 끝냈다.자료가 많아서 기한이 지났어요.그래서 제가 겪은 몇 가지 문제를 여러분께 말씀드리겠습니다.
nativeEvent는 QWidget의 허함수입니다. 이 부분에 대한 Qt5 문서가 여기 있습니다.Qt5는 C++ 멀티플랫폼의 GUI 라이브러리입니다.http://doc.qt.io/qt-5/qwidget.html#nativeEventPyQt는 Python을 대상으로 합니다.사용하기에 약간 다르고 관련 문서도 완전하지 않고 체계화되지 않아 사용할 때 찾을 수밖에 없다.
인터넷에서 찾은 MSG.from_address (msg.init ()) 는 내 환경에서 실행할 수 없습니다.제가 한번 해봤는데 MSG를 사용할 수 있어요.from_address (int (msg)) 인데, 무슨 이유인지 잘 모르겠어요.
nativeEvent 에 대한 반환 값입니다.어떤 자료들은 반드시 False가 되어야 한다고 말한다. 그렇지 않으면 문제가 생길 수 있다.이거 안 해봤어요.두 번째 반환값도 자료에 기입한 것이다.운행은 성공할 수 있지만, 이 부분에 익숙한 사람이 있다면, 답장을 환영합니다.
내가 찾아본 몇 가지 자료:https://blog.csdn.net/hidxl/article/details/53633874 https://blog.csdn.net/neverstop_2009/article/details/47361309 https://blog.csdn.net/swqqcs/article/details/7251321 ———————————————— https://blog.csdn.net/josephzhang1012/article/details/86694876

좋은 웹페이지 즐겨찾기