之前看到了一个例子是 QLineEdit setCompleter 的 在QLineEdit上输入就会显示输入提示
例子还说了 QComboBox也有一个以同样工作方式的 setCompleter() 方法.
于是我就想试一下
comboBox->setCompleter(directoryCompleter); //directoryCompleter 是一个模型指针
comboBox->setEditable(true); //comboBox默认是不可编辑的 设置为可编辑的
但是程序运行后没有达到应有的效果
究竟是什么原因呢 ? 查看了一下文档
void QComboBox::setCompleter ( QCompleter * completer )
Sets the completer to use instead of the current completer. If completer is 0, auto completion is disabled.
By default, for an editable combo box, a QCompleter that performs case insensitive inline completion is automatically created.
"for an editable combo box"
根据这句话我就把两行代码换了一下位置
comboBox->setEditable(true); //comboBox默认是不可编辑的 设置为可编辑的
comboBox->setCompleter(directoryCompleter); //directoryCompleter 是一个模型指针
然后程序达到了我想要的效果
希望遇到此问题的同志能顺利解决
一定先要让comboBox可编辑,再 setCompleter();
令 comboBox->setCompleter(QStringList()<<"1234"<<"5678"<<"sdf"); //不传递一个模型 ,传递一个QStringList也是可以的