2014-08-29 116 views
2

我正在设置一个click()事件到QLineEdit,我已经成功地做到了。但是当QLine Edit被点击时我想回到Mainwindow,因为我需要Mainwindow中的数据来进一步处理数据。但是我没有让它回归,也没有引用主窗口作为父母,我希望有人能指出。非常感谢。添加一个点击QLineEdit

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 
+0

您可以对您的主窗口的代码更多的细节?我无法理解你的意思,“我需要Mainwindow中的数据来进一步处理数据,但是我没有让它返回,也没有将Mainwindow作为父项引用”? – ashwinjv 2014-08-29 02:54:19

+0

我的意思是一旦QLineEdit被点击,我将使用MainWindow的变量中的一些数据并将其显示在其他QTextEdit中。所以我需要在MainWindow中处理数据,所以我可以使用数据 – EricBkc 2014-08-29 15:28:06

回答

1

只需简单地调用MainWindowmousePressEvent,并给它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) 
+0

嗨,大卫,谢谢,但我只是尝试过,但它不起作用,首先,我必须把QtGui.QLineEdit而不是QLineEdit放在括号内,否则会出现错误。其次,MyLineEdit没有父变量。所以它不能转发到主窗口。 – EricBkc 2014-08-29 15:24:56

+0

我编辑了我的答案。如果你不能将'QLineEdit'作为父项,请尝试第二项。 – 2014-08-29 17:16:12

+0

文件“/ Users/cvorg/vel/VEL Programmer/Aug_29th.py”,行1486,在MyLineEdit mousePressed = QtCore.pyqtProperty(QtCore.QMouseEvent) AttributeError:'module'对象没有属性'QMouseEvent' – EricBkc 2014-08-29 19:47:07

0

这是我用来为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做的onClick:

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

专门为OP:

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