Add a click on QLineEdit

后端 未结 3 406
清酒与你
清酒与你 2021-01-15 06:32

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 i

相关标签:
3条回答
  • 2021-01-15 06:57

    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)
    
    0 讨论(0)
  • 2021-01-15 07:04

    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)
    
    0 讨论(0)
  • 2021-01-15 07:12

    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)
    
    0 讨论(0)
提交回复
热议问题