EricBkc
EricBkc

Reputation: 399

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

Upvotes: 2

Views: 11674

Answers (3)

steveo225
steveo225

Reputation: 11892

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

class ClickableLineEdit(QLineEdit):
    clicked = pyqtSignal() # signal when the text entry is left clicked

    def mousePressEvent(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)

Upvotes: 5

David Ludwig
David Ludwig

Reputation: 1037

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)

Upvotes: 3

ashwinjv
ashwinjv

Reputation: 2967

This is what I used to do onClick for QLineEdits

class MyLineEdit(QtGui.QLineEdit):

    def focusInEvent(self, e):
        try:
            self.CallBack(*self.CallBackArgs)
        except AttributeError:
            pass
        super().focusInEvent(e)

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

    def SetCallBackArgs(self, args):
        self.CallBackArgs = args

and in my MainGUI:

class MainGUI(..):

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

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

Upvotes: 0

Related Questions