【问题标题】:QLineEdit hover-over Signal - when mouse is over the QlineEditQLineEdit 悬停信号 - 当鼠标悬停在 QlineEdit
【发布时间】:2014-03-20 21:59:41
【问题描述】:

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

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

cursorPositionChanged (int old, int new)
编辑完成()
returnPressed ()
selectionChanged ()
textChanged ( const QString & text )
textEdited (const QString & text)

但是,这些都不是专门用于悬停的。您能否建议这是否可以在 PyQt4 中通过任何其他方式完成?

【问题讨论】:

    标签: python-2.7 pyqt4 mouseover qlineedit


    【解决方案1】:

    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_())
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 2018-09-03
        • 1970-01-01
        • 2015-04-22
        • 2011-05-26
        • 2017-10-14
        • 1970-01-01
        相关资源
        最近更新 更多