Skip to content Skip to sidebar Skip to footer

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 Mainw

Solution 1:

Just simply call the MainWindowmousePressEvent and give it the event variable the line edit received

classMyLineEdit(QtGui.QLineEdit):def__init__(self, parent):

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

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

Or you can connect a signal from the line edit

classMyLineEdit(QtGui.QLineEdit):

    mousePressed = QtCore.pyqtProperty(QtGui.QMouseEvent)

    def__init__(self, value):

        super(MyLineEdit, self).__init__(value)

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

Solution 2:

I use the following to connect any method as the callback for a click event:

classClickableLineEdit(QLineEdit):
    clicked = pyqtSignal() # signal when the text entry is left clickeddefmousePressEvent(self, event):
        if event.button() == Qt.LeftButton: self.clicked.emit()
        else: super().mousePressEvent(event)

To use:

textbox = ClickableLineEdit('Default text')
textbox.clicked.connect(someMethod)

Specifically for the op:

self.tc = ClickableLineEdit(self.field[con.ConfigFields.VALUE])
self.tc.clicked.connect(self.mouseseleted)

Solution 3:

This is what I used to do onClick for QLineEdits

classMyLineEdit(QtGui.QLineEdit):

    deffocusInEvent(self, e):
        try:
            self.CallBack(*self.CallBackArgs)
        except AttributeError:
            passsuper().focusInEvent(e)

    defSetCallBack(self, callBack):
        self.CallBack = callBack
        self.IsCallBack = True
        self.CallBackArgs = []

    defSetCallBackArgs(self, args):
        self.CallBackArgs = args

and in my MainGUI:

classMainGUI(..):

    def__init__(...):
        ....
        self.input = MyLineEdit()
        self.input.SetCallBack(self.Test)
        self.input.SetCallBackArgs(['value', 'test'])
        ...

    defTest(self, value, test):
        print('in Test', value, test)

Post a Comment for "Add A Click On Qlineedit"