2014-08-29 74 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)