问题是:我有QListWidget,选择模式允许通过单击一个,拖动和释放另一个来选择多个项目。我希望在屏幕上任何地方释放鼠标时发出信号(仅当它首先被按下列表中的某个项目时)。我该怎么做?
到目前为止我调查的内容: QListWidget具有信号itemSelectionChanged,但是在拖动项目时选择每个新项目后发出信号
QListWidget点击了从QAbstractItemView继承的信号,但只有在同一项目上推送和释放鼠标时才有效
我试图扩展QListWidget类并在其中定义mouseReleaseEvent,但是这会覆盖原始事件并破坏列表的正确行为
我曾尝试使用installEventFilter创建空白标签,并创建类mouseReleasedFilter,并且以某种方式工作,但是有更优雅的方式吗?
class mouseReleasedFilter(QtCore.QObject):
def __init__(self, parent = None):
super(mouseReleasedFilter, self).__init__(parent)
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseButtonRelease:
print 'released'
return False
return True
self.filter = mouseReleasedFilter(self)
self.label.installEventFilter(self.filter)
我将不胜感激任何帮助
答案 0 :(得分:1)
我试图扩展QListWidget类并在其中定义mouseReleaseEvent,但是这会覆盖原始事件并破坏列表的正确行为
这是正确的方法;只需运行您使用super
覆盖的方法:
class MyList(QtGui.QListWidget):
def mouseReleaseEvent(self, e):
super(MyList, self).mouseReleaseEvent(e)
print('released', e)
示例:
from PyQt4 import QtGui
app = QtGui.QApplication([])
class MyList(QtGui.QListWidget):
def mouseReleaseEvent(self, e):
super(MyList, self).mouseReleaseEvent(e)
print('released', e)
l = MyList()
l.addItems(['hello', 'world'])
l.show()
输出:
('released', <PyQt4.QtGui.QMouseEvent object at 0x72b4d40>)
('released', <PyQt4.QtGui.QMouseEvent object at 0x72b4d40>)
('released', <PyQt4.QtGui.QMouseEvent object at 0x72b4d40>)
('released', <PyQt4.QtGui.QMouseEvent object at 0x72b4d40>)
('released', <PyQt4.QtGui.QMouseEvent object at 0x72b4d40>)