QComboBox不区分大小写自动补全功能异常问题求助
Qt可编辑QComboBox大小写输入补全异常问题
#include <QApplication> #include <QComboBox> #include <QLineEdit> QStringList slNames={ "anna", "andrew", "ashley", "brian", "brianna", "brandon", "cameron", "daniel", "danna", "devon", "derek", "emma" }; int main(int argc,char *argv[]) { QApplication appMain(argc,argv); QComboBox cboNames; cboNames.setEditable(true); QObject::disconnect( cboNames.lineEdit(), &QLineEdit::editingFinished, nullptr, nullptr ); // Added (2023-01-25) QObject::connect( cboNames.lineEdit(), &QLineEdit::textEdited, [&](const QString &sText) { //cboNames.lineEdit()->blockSignals(true); Removed (2023-01-25) cboNames.clear(); cboNames.hidePopup(); if(sText.length()) { for(const auto &n:slNames) if(n.startsWith(sText,Qt::CaseInsensitive)) cboNames.addItem(n); cboNames.showPopup(); cboNames.setEditText(sText); cboNames.lineEdit()->grabKeyboard(); } //cboNames.lineEdit()->blockSignals(false); Removed (2023-01-25) } ); cboNames.show(); return appMain.exec(); }
输入小写"d"时,可正常显示4个结果并实现自动补全;但输入大写"D"时,虽能显示结果,但自动补全功能失效,会直接选中第一个完整结果。已知这与QCompleter有关,但未能成功解决该问题,求解决思路。
编辑(2023-01-25)
尝试使用QCompleter(该组件在独立QLineEdit中表现出色,但无法完美适配QComboBox)后,发现无论是否使用blockSignals(),textEdited/textChanged信号都会被触发。
曾使用cboNames.lineEdit()->disconnect();解决问题,但最终发现是editingFinished信号导致的异常。目前未发现断开该信号方法的弊端,虽然操作看似奇怪,但代码量更少且有效。
注:该程序仅为最小可复现示例,实际列表内容为动态加载。
内容的提问来源于stack exchange,提问作者Alvein
相关产品推荐
相关产品推荐

