我正在尝试使用QCompleter创建一个菜单应用程序(例如Windows搜索)。
当QLineEdit为空时,我想显示来自完成器的所有项目。
它是第一次工作,但是当我开始在lineEdit
中输入内容并删除lineEdit
中的所有字符,然后按Enter
时,我什么也看不到。我的错误在哪里?
我的代码在下面。
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
this->wordList << "alpha" << "omega" << "omicron" << "zeta" << "icon";
this->lineEdit = new QLineEdit(this);
completer = new QCompleter(wordList, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
lineEdit->setCompleter(completer);
completer->QCompleter::complete();
ui->setupUi(this);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter))
{
if(lineEdit->text()=="")
{
completer->complete();
}
if(wordList.contains(lineEdit->text(),Qt::CaseInsensitive))
qDebug() <<"CATCH IT";
}
}
你能告诉我吗?
答案 0 :(得分:0)
您需要在完成程序上重置完成前缀。
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
if(lineEdit->text().isEmpty())
{
lineEdit->completer()->setCompletionPrefix("");
lineEdit->completer()->complete();
}
}
如果仅当在行编辑中按回车键时才填充该意图,则您将需要创建自己的行编辑来处理它,而不是使用主窗口。