pyqt5 中的 Qcompleter 自动完成在我键入时不显示选项

Qcompleter autocomplete in pyqt5 not showing options as i type

我使用 Qcombobox 添加了一个下拉菜单。 它有三个选项:B4、B4.5、B5 如果用户选择 B4.5 并开始在 QlineEdit 中键入,自动填充选项应该基于列表“名称”出现,但它不会发生。我究竟做错了什么 ? 这是代码:

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

嗯,老实说,有很少的地方是错误的。

  1. 你正在创建一个你没有使用的子 QMainWindow(而且一个子 main window 很少被使用);
  2. 您正在创建一个布局,但您没有使用它,因为您没有向它添加小部件;
  3. QMainWindow 支持设置布局,应该使用 central widget(有自己的布局);
  4. design_manual_select在启动时调用,但理论上应该在用户交互时调用该函数;
  5. 该函数不断创建一个新的 QLineEdit,在其上设置了完成者,并且永远不会显示 new 行编辑(在构造函数中使用父级创建的小部件和 未添加到布局 如果父级已经可见则不会自动显示);
  6. 如果当前组合项目 不是 B4.5;
  7. ,则没有重置完成者的函数

所以,让我们解决所有问题:

  1. 必须将 QWidget 设置为 central widget,并且必须为其设置布局;
  2. 必须将小部件添加到该布局,只需添加一次;
  3. 由于当前文本更改时必须更改完成者,因此如果文本不匹配,则必须正确更新;
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())