How to implement a “clicked” event for QLineEdit with python?
There is no “clicked.connect()” for QLineEdit.
How to implement a “clicked” event for QLineEdit with python?
There is no “clicked.connect()” for QLineEdit.
You can use mousePressEvent
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLineEdit
class LineEditClickable(QLineEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.setPlaceholderText("Click here")
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
print("Left click")
elif event.button() == Qt.MouseButton.RightButton:
print("Right click")
super().mousePressEvent(event)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("LineEdit Click")
self.setFixedSize(400, 150)
layout = QVBoxLayout()
self.line_edit = LineEditClickable()
layout.addWidget(self.line_edit)
layout_container = QWidget()
layout_container.setLayout(layout)
self.setCentralWidget(layout_container)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())