rhapsody
rhapsody

Reputation: 25

Qcompleter autocomplete in pyqt5 not showing options as i type

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

Upvotes: 1

Views: 603

Answers (1)

musicamante
musicamante

Reputation: 48231

Well, to be honest, there are a few things that are wrong.

  1. you're creating a child QMainWindow that you're not using (and a child main window is rarely used anyway);
  2. you're creating a layout, but you're not using it since you're not adding widgets to it;
  3. QMainWindow does not support setting layouts anyway, and a central widget (with its own layout) should be used instead;
  4. design_manual_select is called on startup, but such function should be theoretically called upon user interaction;
  5. that function continuously creates a new QLineEdit on which the completer is set, and that new line edit is never shown anyway (widgets created with a parent in the constructor and not added to a layout are not automatically shown if the parent is already visible);
  6. there's no function that resets the completer if the current combo item is not B4.5;

So, let's fix all that:

  1. a QWidget must be set as a central widget, and a layout must be set for it;
  2. widgets must be added to that layout, and just once;
  3. since the completer must change upon current text change, it must be properly updated if the text doesn't match;
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())

Upvotes: 3

Related Questions