I am using PyQt based on PyQt4. I am using PyCharm 2017.3. My python version is 3.4.
I am trying to connect the signal that we get when we click mouse to capture content from a QLineEdit.
class HelloWorld(QMainWindow, tD_ui.Ui_MainWindow):
# defining constructor
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.getContent()
self.putValues()
self.setWindowTitle("Downloader")
self.pushButton.mousePressEvent.connect(self.getContent)
So when I run the code.The following error shows up
Traceback (most recent call last):
File "C:/Project/Downloader/Implement.py", line 113, in <module>
helloworld = HelloWorld()
File "C:/Project/Downloader/Implement.py", line 18, in __init__
self.pushButton.mousePressEvent.connect(self.getContent)
AttributeError: 'builtin_function_or_method' object has no attribute 'connect'
P.S-> Please try to avoid legacy codes in solution
mousePressEvent
is not a signal so you should not use connect, which you should use the clicked
signal:
self.pushButton.clicked.connect(self.getContent)
Plus:
In Qt, therefore for PyQt, there are signals and events, signals are emitted and events must be overwritten, in the case of buttons, the clicked task is natural and inherent in its logic, so this signal has been created, but in the case of a QLabel does not have that signal but we can use a mousePressEvent event to generate that signal as I show below:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Label(QLabel):
clicked = pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = Label("click me")
w.clicked.connect(lambda: print("clicked"))
w.show()
sys.exit(app.exec_())