我有一个QWidget,它包含QLabel和QLineEdits。
当我点击QLabel时,我可以在QWidget中使用mousePressEvent。但是当我点击QLineEdit时,我无法在QWidget中检测到mousePressEvent - 仅在QLineEdit中。我认为它与QLineEdit的工作方式有关 - 我不知道在整个地区内获取鼠标事件的方法。
修改:
我为Maya制作了一个自定义频道框,如上所述。我尝试通过拖动鼠标来选择多个通道。但正如我所提到的,在QLineEdit区域我无法做到这一点。
class channelWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(channelWidget, self).__init__(parent)
self.resize(180, 20)
self.setMinimumSize(180, 20)
self.setMaximumHeight(20)
self.attr_label = QtGui.QLabel(self)
self.attr_label.setGeometry(QtCore.QRect(5, 0, 110, 20))
self.attr_label.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.value_field = focusLineEdit(self)
self.value_field.setGeometry(QtCore.QRect(120, 0, 60, 20))
self.value_field.setAlignment(QtCore.Qt.AlignLeft|QtCore.Qt.AlignLeading|QtCore.Qt.AlignVCenter)
self.value_field.setValidator(QtGui.QDoubleValidator())
每个元素由QLabel和QLineEdit组成。
class channelContainerWidget(QtGui.QWidget):
def updateChannel(self, node="", attrList=[]):
_l = self.channel_layout
_list = []
for w in [_l.itemAt(i).widget() for i in range(_l.count()) if _l.itemAt(i).widget()]:
if w in self._selectList: _list.append( str( w.attr_label.text() ) )
sip.delete(w)
_selList = []
for _id, at in enumerate(attrList):
_item = channelWidget(self)
_item.attr_label.setText(at)
_item.value_field.setText(value)
_l.insertWidget(_id, _item)
包含的小部件如上所述。当我点击QLabel区域时,我可以获得鼠标事件,但是当我点击QLineEdit区域时,我不能。
答案 0 :(得分:3)
如果您正在观看包含布局的主窗口小部件中的mousePressEvent()
,那么您看到QLabel
点击次数的原因是默认情况下QLabel
会忽略mousePressEvent
并允许它冒泡到下一个父级。事件从child-> parent向上传播。
但是QLineEdit
确实需要接受并使用鼠标按下来处理焦点以及线编辑小部件常用的其他各种动作。
对你来说真正可能的选择是使用event filter。这将允许您的主窗口小部件监视其他窗口小部件的事件,而无需继承QLineEdit
并实现mousePressEvent
在这个例子中,它只是监视布局的任何成员和鼠标按下,然后进行打印,然后执行默认操作以不更改任何内容:
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.layout = QtGui.QHBoxLayout(self)
self.label = QtGui.QLabel("Label")
self.line = QtGui.QLineEdit()
self.layout.addWidget(self.label)
self.layout.addWidget(self.line)
# have this widget (self) watch events for these
self.label.installEventFilter(self)
self.line.installEventFilter(self)
def mousePressEvent(self, event):
print "Main Widget Mouse Press"
super(Widget, self).mousePressEvent(event)
def eventFilter(self, obj, event):
# you could be doing different groups of actions
# for different types of widgets and either filtering
# the event or not.
# Here we just check if its one of the layout widgets
if self.layout.indexOf(obj) != -1:
if event.type() == event.MouseButtonPress:
print "Widget click", obj
# if I returned True right here, the event
# would be filtered and not reach the obj,
# meaning that I decided to handle it myself
# regardless, just do the default
return super(Widget, self).eventFilter(obj, event)
测试此代码时,您会看到单击标签会导致主窗口小部件中的打印及其事件过滤器。但是单击行编辑只会导致事件过滤器打印语句,因为默认mousePressEvent
接受它并且不会进一步传播它。