Why is a QLineEdit with a QCompleter not shown to auto-complete?

I have two QLineEdit

in the program: lineEdit

and fileName_Edit

. lineEdit

contains the path to the directory (taken from the user). The user then enters the filename into the fileName_Edit

. I want to show suggestions to the user when he enters a filename in fileName_Edit

. I tried to implement QCompleter

like this:
( dirContents

is that QStringList

which contains the contents of the directory specified by the user in 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);
}

      

The program compiles successfully, but no completion appears. Even if I try to connect a signal textChanged

to a function like below, it doesn't appear.

QObject::connect(&MainWindow::ui->lineEdit, SIGNAL(&textChanged(QString)), this,SLOT(&MainWindow::on_lineEdit_editingFinished())); 

      

EDIT: Adding the above line results in an error:

The expected constructor, destructor, or type conversion to (token

Any help would be greatly appreciated.

+3


source to share


2 answers


Try to keep your code simple, if the following code works for you then everything is fine and start improving it.

In the 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 works on your computer, you will be sure that your system and project is good and start improving it (changelog, etc.). And try not to use global variables.

If you want to do it dynamically. Create a simple model and when you set a new stringList for it, your instance can always display new data



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);

      

If you want to change the data, for example when the user clicks a button or something similar, follow these steps:

QDir dir("G:/2/tmp");
mdl->setStringList(dir.entryList(QStringList(), QDir::Files));

      

Your add-on now has new data

+2


source


Convert comment to response as requested ...

Try installing the add-on before presenting the QLineEdit to the user. For example, in the MainWindow constructor. Wrong to set it in the 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
}

      

+2


source







All Articles