QLineEdit悬停信号 - 当鼠标悬停在QlineEdit上时

问题描述 投票:1回答:2

我有一个QLineEdit,我需要知道是否有一个信号可以跟踪鼠标悬停在QLineEdit上,一旦鼠标结束,QLineEdit就会发出信号。

我看过这些文件,发现我们有以下信号:

cursorPositionChanged(int old,int new) editingFinished() returnPressed() selectionChanged() textChanged(const QString&text) textEdited(const QString&text)

但是,这一切都不是为了悬停。您是否可以建议在PyQt4中是否可以通过任何其他方式完成此操作?

python-2.7 pyqt4 mouseover qlineedit
2个回答
2
投票

QLineEdit没有内置的鼠标悬停信号。

但是,通过安装event-filter很容易实现类似的功能。这种技术适用于任何类型的小部件,您可能需要做的唯一其他事情是set mouse tracking(虽然这似乎默认为QLineEdit打开)。

下面的演示脚本显示了如何跟踪各种鼠标移动事件:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.edit = QtGui.QLineEdit(self)
        self.edit.installEventFilter(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.edit)

    def eventFilter(self, source, event):
        if source is self.edit:
            if event.type() == QtCore.QEvent.MouseMove:
                pos = event.globalPos()
                print('pos: %d, %d' % (pos.x(), pos.y()))
            elif event.type() == QtCore.QEvent.Enter:
                print('ENTER')
            elif event.type() == QtCore.QEvent.Leave:
                print('LEAVE')
        return QtGui.QWidget.eventFilter(self, source, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 300, 100)
    window.show()
    sys.exit(app.exec_())

1
投票

您可以使用enterEventleaveEvent,当鼠标进入窗口小部件时触发enterEvent,当鼠标离开窗口小部件时触发事件。这些事件在QWidget类中,QLineEdit继承QWidget,因此您可以在QLineEdit中使用这些事件。我没有在QLineEdit的文档中看到这些事件,请单击链接列表中的所有成员,包括页面顶部的继承成员。

© www.soinside.com 2019 - 2024. All rights reserved.