Search code examples
pythonqtqlineedit

Add a click on QLineEdit


I am working on set a click() event to QLineEdit, I already successfully did it. But I want to go back to Mainwindow when the QLine Edit is clicked because I need the data in Mainwindow to further process the data. But I failed to let it go back, neither nor to cite the Mainwindow as parent, I hope someone can point it out. Thank you so much.

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

Solution

  • Just simply call the MainWindow mousePressEvent and give it the event variable the line edit received

    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)
    

    Or you can connect a signal from the line edit

    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)
    

    Then just connect the signal in your main window where you created it

        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)