I've added a dropdown using Qcombobox. It has three options: B4, B4.5, B5 If user selects B4.5 and starts typing in the QlineEdit, the autopopulate options should come up based on the list "names" but it doesn't happen. What am I doing wrong ? Here's the code :
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QCompleter
import sys
from PyQt5.Qt import QApplication, QLabel, QLineEdit
class KnowledgeBaseGui(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QMainWindow(self)
self.layout = QGridLayout()
self.setLayout(self.layout)
self.setGeometry(500, 400, 650, 650)
self.setWindowTitle("pyqt5")
self.nand_cmd_menu_current_text = None
def cmd_button(self):
self.combo_cmd = QComboBox(self)
self.combo_cmd.setGeometry(80, 175, 115, 35)
self.combo_cmd.setFont(QFont('Times', 11))
self.combo_cmd.setStyleSheet("background-color: rgb(166,180,242);border: 2px solid rgb(20,20,20)")
self.combo_cmd.addItems(["CMDs", "B4.5", "B5", "B6"])
self.combo_cmd.currentTextChanged.connect(self.design_manual_select)
pass_val = self.design_manual_select(self.nand_cmd_menu_current_text)
def design_manual_select(self, val):
names = ["apple", "alpha", "beta", "blackberry", "charlie", "delta", "chilton", "dawn"]
cmd_lineEdit = QLineEdit(self)
cmd_lineEdit.setGeometry(200, 175, 150, 35)
if val == "B4.5":
print(val)
completer = QCompleter(names)
cmd_lineEdit.setCompleter(completer)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv) # To create the App
app.setStyleSheet("QMainWindow{"
"background-image:url(abc.jpg); background-color: rgb(130,130,125);"
"border: 1px solid rgb(1,1,1)}")
CallKnowledgeBaseGui = KnowledgeBaseGui()
CallKnowledgeBaseGui.cmd_button()
CallKnowledgeBaseGui.show()
sys.exit(app.exec()) # To start the App
Well, to be honest, there are a few things that are wrong.
design_manual_select
is called on startup, but such function should be theoretically called upon user interaction;B4.5
;So, let's fix all that:
class KnowledgeBaseGui(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QWidget()
self.setCentralWidget(self.mwidget)
self.layout = QGridLayout(self.mwidget)
self.setGeometry(500, 400, 650, 650)
self.setWindowTitle("pyqt5")
self.combo_cmd = QComboBox()
self.combo_cmd.setFont(QFont('Times', 11))
self.combo_cmd.setStyleSheet("background-color: rgb(166,180,242);border: 2px solid rgb(20,20,20)")
self.combo_cmd.addItems(["CMDs", "B4.5", "B5", "B6"])
self.layout.addWidget(self.combo_cmd)
self.combo_cmd.currentTextChanged.connect(self.design_manual_select)
self.cmd_lineEdit = QLineEdit()
self.layout.addWidget(self.cmd_lineEdit)
def design_manual_select(self, val):
if val == "B4.5":
names = ["apple", "alpha", "beta", "blackberry", "charlie", "delta", "chilton", "dawn"]
self.cmd_lineEdit.setCompleter(QCompleter(names))
else:
self.cmd_lineEdit.setCompleter(None)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{"
"background-image:url(abc.jpg); background-color: rgb(130,130,125);"
"border: 1px solid rgb(1,1,1)}")
callKnowledgeBaseGui = KnowledgeBaseGui()
callKnowledgeBaseGui.show()
sys.exit(app.exec())