PYQT 사용, 사용자 정의 목록이있는 Qcombobox 용 Mousepressevent를 어떻게 필터링합니까?
문제
나는 가지고있다 QComboBox
사용자 정의 목록 개체와 함께.
사용자 정의 목록 객체에는 사용자 정의가 있습니다 mousePressEvent
따라서 사용자가 +/- (비틀기)가있는 원 중 하나를 클릭하면 목록이 확장/붕괴됩니다.
콤보 상자와 함께 목록을 사용하면 사용자가 트위스트를 클릭하면 목록이 확장/붕괴되지만 선택이 변경되고 목록이 숨겨져 있습니다. 사용자가 트위스트를 클릭하면 선택이 변경되지 않고 목록이 숨겨지지 않도록 어떻게 필터링 할 수 있습니까?
추가 스크린 샷
모든 노드가 무너졌습니다.
숨겨진 목록 :
해결책
QT에는 a eventFilter
그 "캡처" QEvent.MouseButtonRelease
. 그래서 내가 한 일은 내 자신의 설치입니다 eventFilter
그 필터 QEvent.MouseButtonRelease
이벤트 사용자가 노드를 클릭하면 이벤트.
내 목록 개체에는 다음 방법이 있습니다.
def mousePressEvent (self, e):
self.colapse_expand_click = False
if <user clicked node>:
colapse_expand_node()
e.accept ()
self.colapse_expand_click = True
그만큼 mousePressEvent
전에 실행됩니다 mouseReleaseEvent
.
그런 다음 커스텀 콤보 박스에서 이벤트를 필터링합니다.
class RevisionSelectorWidget(QtGui.QComboBox):
def __init__(self, parent = None):
QtGui.QComboBox.__init__(self, parent)
self.log_list = RevisionSelectorLogList(self)
self.setView(self.log_list)
self.log_list.installEventFilter(self)
self.log_list.viewport().installEventFilter(self)
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseButtonRelease:
if self.log_list.colapse_expand_click:
return True
return False
다른 팁
내 머리 꼭대기에서, 당신은 서브 클래스를 할 수 있습니다 QComboBox
그리고 무시합니다 hideEvent(QHideEvent)
(상속 QWidget
)
def hideEvent(self, event):
if self.OkToHide():
event.accept()
else:
event.ignore()
스크린 샷은 콤보 상자의 흥미로운 사용처럼 보입니다. 왜 당신이 왜 사용하지 않았는지 궁금합니다. TreeView
목록 대신 스타일 제어?
편집 (2009 년 3 월 14 일) :
QT 소스 코드를보고 키보드 및 마우스 이벤트가 캡처 될 때 QT가 방출하기로 결정하자마자 "activated(int index)"
신호, "hidePopup()"
호출되었습니다.
따라서 이벤트 필터 코드를 다시 작성하는 것 외에도 또 다른 옵션은 "activated(int index)"
또는 "highlighted(int index)"
호출 할 수있는 슬롯에 신호를 보냅니다 "showPopup()"
목록 항목을 다시 표시 할 것입니다. 불쾌한 사라지거나 페인트 문제가 발생하면 팝업이 보이는 동안 QT가 페인트 이벤트를 지연 시키도록해야 할 수도 있습니다.
도움이되기를 바랍니다!