한국어
파이썬
 

PyQt4 [PyQt4]마우스 클릭 이벤트 예제 코드

pjk 2014.08.26 17:27 조회 수 : 6775


from PyQt4 import QtCore, QtGui

class CustomButton(QtGui.QPushButton):

    left_clicked= QtCore.pyqtSignal(int)
    right_clicked = QtCore.pyqtSignal(int)

    def __init__(self, *args, **kwargs):
        QtGui.QPushButton.__init__(self, *args, **kwargs)
        self.timer = QtCore.QTimer()
        self.timer.setInterval(250)
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.timeout)
        self.left_click_count = self.right_click_count = 0

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.left_click_count += 1
            if not self.timer.isActive():
                self.timer.start()
        if event.button() == QtCore.Qt.RightButton:
            self.right_click_count += 1
            if not self.timer.isActive():
                self.timer.start()

    def timeout(self):
        if self.left_click_count >= self.right_click_count:
            self.left_clicked.emit(self.left_click_count)
        else:
            self.right_clicked.emit(self.right_click_count)
        self.left_click_count = self.right_click_count = 0


class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        self.button1 = CustomButton("Button 1")
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.button1)
        self.setLayout(hbox)
        self.button1.left_clicked[int].connect(self.left_click)
        self.button1.right_clicked[int].connect(self.right_click)

    def left_click(self, nb):
        if nb == 1: print('Single left click')
        else: print('Double left click')

    def right_click(self, nb):
        if nb == 1: print('Single right click')
        else: print('Double right click')


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MyDialog()
    w.show()
    sys.exit(app.exec_())

번호 제목 글쓴이 날짜 조회 수
24 외부 프로그램 실행 pjk 2015.02.10 15296
23 [PyQt4]여러가지 버튼 예제 file pjk 2014.08.29 13721
22 [PyQt4]폴더 또는 파일을 드레그하여 그 경로를 LineEdit로 가져오기 file pjk 2014.08.22 13528
21 [pyqt4]QTimer 예제 - 버튼을 누르면 3초후 함수 또는 메소드 호출 makersweb 2015.04.01 9640
20 Boost Python을 이용하여 python을 위한 C++ API 바인딩 [5] file makersweb 2017.01.08 9171
19 [Python]EXE또는 DLL파일의 버전정보를 알아내기위한 몇가지 방법 makersweb 2015.06.25 8695
18 [PyQt4]스레드 및 ProgressBar 예제 코드 file pjk 2014.08.26 8251
17 [PyQt4]multiprocess 예제 코드 pjk 2014.08.26 8146
16 [PyQt4]윈도우창에 별 찍기 예제 file pjk 2014.08.19 8135
15 Python으로 작성된 프로그램을 윈도우응용프로그램(exe)으로 빌드하기 file pjk 2014.08.03 8090
14 Python + QML with PyQt4 makersweb 2015.03.24 7891
13 [PyQt4]개발 프로그램 버전표시 메세지 박스 pjk 2014.09.02 7684
12 파이썬으로 작성된 소스를 py2exe을 이용하여 윈도우 응용프로그램 빌드시 콘솔창 숨기기 pjk 2014.07.29 7592
» [PyQt4]마우스 클릭 이벤트 예제 코드 pjk 2014.08.26 6775
10 Python 문자열 관련 함수 레퍼런스 pjk 2014.08.29 6618
9 print를 로그파일로 생성하기 (log출력 Redirection) makersweb 2015.03.18 5618
8 Qt For Python(PySide2) QML 프로젝트 예제 file makersweb 2019.10.17 4801
7 How to Use Freeze pjk 2014.09.06 4751
6 다른 디렉터리의 파일(모듈) import 하기 pjk 2014.08.22 4692
5 QML 및 Window 투명처리 file makersweb 2015.04.22 4028