python – 在QLineEdit上添加一个单击

我正在设置一个click()事件到QLineEdit,我已经成功地做到了.但是当我点击QLine Edit时我想回到Mainwindow,因为我需要Mainwindow中的数据来进一步处理数据.但我没有让它回去,也没有引用Mainwindow作为父母,我希望有人可以指出它.非常感谢.

MainWindow
{

...


self.tc = MyLineEdit(self.field[con.ConfigFields.VALUE])#self.tc = wx.TextCtrl(self.parent, -1, str(field[con.ConfigFields.VALUE]), pos=(x+220, y-3), size=(200, -1))

...

}


class MyLineEdit(QtGui.QLineEdit):

    def __init__(self, parent=MainWindow):
        super(MyLineEdit, self).__init__(parent)
        #super(CustomQLineEidt, self).__init__()


    def mousePressEvent(self, e):
        self.mouseseleted()

    def mouseseleted(self):
        print "here"
        MainWindow.mousePressEvent

解决方法:

只需简单地调用MainWindow mousePressEvent,并为其提供行编辑收到的事件变量

class MyLineEdit(QtGui.QLineEdit):

    def __init__(self, parent):

        super(MyLineEdit, self).__init__(parent)
        self.parentWindow = parent

    def mousePressEvent(self, event):
        print 'forwarding to the main window'
        self.parentWindow.mousePressEvent(event)

或者您可以从线路编辑中连接信号

class MyLineEdit(QtGui.QLineEdit):

    mousePressed = QtCore.pyqtProperty(QtGui.QMouseEvent)

    def __init__(self, value):

        super(MyLineEdit, self).__init__(value)

    def mousePressEvent(self, event):
        print 'forwarding to the main window'
        self.mousePressed.emit(event)

然后,只需在创建它的主窗口中连接信号即可

    self.tc = MyLineEdit(self.field[con.ConfigFields.VALUE])#self.tc = wx.TextCtrl(self.parent, -1, str(field[con.ConfigFields.VALUE]), pos=(x+220, y-3), size=(200, -1))
    self.tc.mousePressed[QtGui.QMouseEvent].connect(self.mousePressEvent)
上一篇:[python]PyQt5- QLineEdit综合案例


下一篇:PyQt5基础控件