我正在尝试用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";
}
}你能告诉我吗?
发布于 2019-09-25 22:23:12
您需要在完成器上重置完成前缀。
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
if(lineEdit->text().isEmpty())
{
lineEdit->completer()->setCompletionPrefix("");
lineEdit->completer()->complete();
}
}另外,如果只希望在行编辑中按返回时填充它,那么您将希望创建自己的行编辑来处理这个问题,而不是使用主窗口。
https://stackoverflow.com/questions/58100228
复制相似问题