The PYQT5 demo will show a modal custom QDialog window when a button on a QMainWindow is clicked.The QDialog has a QLineEdit with QCompleter.Key Down&Up is available for selecting a popup item, but I can not use mouse to click and select it because the popup view is frozen.I also wanna the popup item text will not add to the QLineEdit automatically when use key Down&Up,only if I use mouse to click the item.
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QVBoxLayout, QDialog, QLineEdit, QCompleter
items_list = ['John Doe', 'Jane Doe', 'Albert Einstein', 'Alfred E Newman']
class SecondDialog(QDialog):
def __init__(self):
super(SecondDialog, self).__init__()
self.v_layout = QVBoxLayout()
self.my_edit = QLineEdit()
self.completer = QCompleter(items_list)
self.my_edit.setCompleter(self.completer)
self.v_layout.addWidget(self.my_edit)
self.setLayout(self.v_layout)
class MyWindows(QMainWindow):
def __init__(self):
super(MyWindows, self).__init__()
self.widget = QWidget()
self.v_layout = QVBoxLayout(self.widget)
self.my_btn = QPushButton("Open second Dialog")
self.my_btn.clicked.connect(self.openDialog)
self.v_layout.addWidget(self.my_btn)
self.setCentralWidget(self.widget)
def openDialog(self):
d = SecondDialog()
d.exec_()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindows()
w.show()
sys.exit(app.exec_())