[英]Why QLineEdit with QCompleter for auto completion doesn't shows up?
I have two QLineEdit
s in the program being lineEdit
and fileName_Edit
.我在程序中有两个
QLineEdit
是lineEdit
和fileName_Edit
。 lineEdit
holds path to a directory (taken from user). lineEdit
保存目录的路径(取自用户)。 Then user enters the name of file in the fileName_Edit
.然后用户在
fileName_Edit
输入文件名。 I want to show suggestions to user when he is entering the file name in fileName_Edit
.当他在
fileName_Edit
输入文件名时,我想向用户显示建议。 I tried to implement QCompleter
like this:我尝试像这样实现
QCompleter
:
( dirContents
is a QStringList
which holds the contents of the directory specified by user in lineEdit
) (
dirContents
是一个QStringList
,它保存了用户在lineEdit
指定的目录的内容)
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
QCompleter *fileEditCompleter = new QCompleter(dirContents, this);
fileEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
fileEditCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
ui->fileName_Edit->setCompleter(fileEditCompleter);
}
Program compiles successfully but, the completer doesn't shows up.程序编译成功,但是完成程序没有出现。 Even if I try to connect
textChanged
signal to the function like following, it doesn't shows up.即使我尝试将
textChanged
信号连接到如下所示的函数,它也不会显示。
QObject::connect(&MainWindow::ui->lineEdit, SIGNAL(&textChanged(QString)), this,SLOT(&MainWindow::on_lineEdit_editingFinished()));
EDIT: Adding above line gives an error saying:编辑:添加上面的行给出了一个错误说:
Expected constructor, destructor or type-conversion before ( token
预期的构造函数、析构函数或类型转换之前(令牌
Any help will be greatly appreciated.任何帮助将不胜感激。
Try to do it simply first, if the code works, then everything is good and you can start improving it.首先尝试简单地做,如果代码有效,那么一切都很好,您可以开始改进它。
In constructor:在构造函数中:
QDir dir("G:/2");//path here
QStringList dirContents = dir.entryList(QStringList(), QDir::Files);
qDebug() << dirContents;//make sure that you list isn't empty, or use isEmpty method
QCompleter *fileEditCompleter = new QCompleter(dirContents, this);
fileEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
fileEditCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
ui->lineEdit->setCompleter(fileEditCompleter);
If this will work on your computer then you can be sure that your system and project are good, and start improve it (change list etc).如果这可以在您的计算机上运行,那么您可以确定您的系统和项目是好的,并开始改进它(更改列表等)。 And try not to use global variables.
并且尽量不要使用全局变量。
If you want do it dynamically, create a simple model and when you will set new QStringList
to it, your completer always will display new data如果你想动态地做,创建一个简单的模型,当你为它设置新的
QStringList
时,你的完成者总是会显示新数据
QDir dir("G:/2");
QStringList dirContents = dir.entryList(QStringList(), QDir::Files);
mdl = new QStringListModel(dirContents,this);//QStringListModel *mdl in header
QCompleter *fileEditCompleter = new QCompleter(mdl, this);
fileEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
fileEditCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
ui->lineEdit->setCompleter(fileEditCompleter);
When you want change data when for example, user clicks button or something else, you can do:当您想要更改数据时,例如,用户单击按钮或其他内容时,您可以执行以下操作:
QDir dir("G:/2/tmp");
mdl->setStringList(dir.entryList(QStringList(), QDir::Files));
Now your completer has new data.现在你的完成者有新的数据。
Converting comment to an answer, as requested...根据要求将评论转换为答案...
Try to set completer before providing QLineEdit to user.在向用户提供 QLineEdit 之前尝试设置完成者。 For example - in constructor of MainWindow.
例如 - 在 MainWindow 的构造函数中。 It is not correct to set it in textChanged slot.
在 textChanged 插槽中设置它是不正确的。
MainWindow::MainWindow()
: QWidget(nullptr)
, ui( new ui_MainWindow() )
{
ui->setupUi(this);
//...
QCompleter *fileEditCompleter = new QCompleter(dirContents, this);
fileEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
fileEditCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
ui->fileName_Edit->setCompleter(fileEditCompleter);
}
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
// Do nothing here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.