QCompleter 中的 QLineEdit 不显示所有项目
QLineEdit in QCompleter doesn't show all items
我正在尝试使用 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";
}
}
你能告诉我吗?
您需要重置完成者的完成前缀。
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
if(lineEdit->text().isEmpty())
{
lineEdit->completer()->setCompletionPrefix("");
lineEdit->completer()->complete();
}
}
此外,如果仅在行编辑中按下 return 时才填充它,您将需要创建自己的行编辑来处理该问题,而不是使用主 window。
我正在尝试使用 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";
}
}
你能告诉我吗?
您需要重置完成者的完成前缀。
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
if(lineEdit->text().isEmpty())
{
lineEdit->completer()->setCompletionPrefix("");
lineEdit->completer()->complete();
}
}
此外,如果仅在行编辑中按下 return 时才填充它,您将需要创建自己的行编辑来处理该问题,而不是使用主 window。