单击QLineEdit

时间:2014-08-29 02:16:44

标签: python qt qlineedit

我正在为QLineEdit设置click()事件,我已经成功完成了它。但是当我点击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

3 个答案:

答案 0 :(得分:1)

只需致电MainWindow mousePressEvent并为其提供收到的行编辑的event变量

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)

答案 1 :(得分:1)

我使用以下方法将任何方法连接为click事件的回调:

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)

使用:

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

专门针对操作:

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

答案 2 :(得分:0)

这就是我以前在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

在我的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)