I needed something just like this! However in my testing I did notice some things, there are some things that you can get rid of (for example, QString post isn't actually used, it can be deleted). Also, there seems to be a bug in your implementation (at least from my testing), if you type in a list of words, then go back to type a word in the middle somewhere, if you use arrow keys to go up/down the autocomplete list, the first highlighted word will be placed in the correct position, but after that the cursor jumps to the end (regardless of the onLineEditTextChanged() slot), and then it starts replacing the last word with those from the autocorrect list.
I created my own implementation, one of the major differences is my implementation takes a delimiter character, and that character is used instead of always using a white space, and I fixed the bug that I described above. You'll notice my implementation is quite more simple:
DelimitedCompleter.hpp
#ifndef DELIMITEDCOMPLETER_HPP
#define DELIMITEDCOMPLETER_HPP
#include <QCompleter>
class QString;
class QStringList;
/**
* class DelimitedCompleter
*
* QCompleter that supports completing multiple words in a QLineEdit, completed words are separated
* by delimiter.
*/
class DelimitedCompleter : public QCompleter {
Q_OBJECT
public:
DelimitedCompleter(QLineEdit *parent, char delimiter);
DelimitedCompleter(QLineEdit *parent, char delimiter, QAbstractItemModel *model);
DelimitedCompleter(QLineEdit *parent, char delimiter, const QStringList &list);
QString pathFromIndex(const QModelIndex &index) const;
QStringList splitPath(const QString &path) const;
private:
char delimiter;
mutable int cursor_pos = -1;
void connectSignals();
private slots:
void onActivated(const QString &text);
void onCursorPositionChanged(int old_pos, int new_pos);
};
#endif
DelimitedCompleter.cpp
#include "DelimitedCompleter.hpp"
#include <QDebug>
#include <QLineEdit>
#include <QStringList>
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DELIMITEDCOMPLETER PUBLIC METHODS *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
DelimitedCompleter::DelimitedCompleter(QLineEdit *parent, char delimiter)
: QCompleter(parent), delimiter(delimiter) {
parent->setCompleter(this);
connectSignals();
}
DelimitedCompleter::DelimitedCompleter(QLineEdit *parent, char delimiter, QAbstractItemModel *model)
: QCompleter(model, parent), delimiter(delimiter) {
parent->setCompleter(this);
connectSignals();
}
DelimitedCompleter::DelimitedCompleter(QLineEdit *parent, char delimiter, const QStringList &list)
: QCompleter(list, parent), delimiter(delimiter) {
parent->setCompleter(this);
connectSignals();
}
QString DelimitedCompleter::pathFromIndex(const QModelIndex &index) const {
QString auto_string = index.data(Qt::EditRole).toString();
QLineEdit *line_edit = qobject_cast<QLineEdit*>(parent());
QString str = line_edit->text();
// If cursor position was saved, restore it, else save it
if(cursor_pos != -1) line_edit->setCursorPosition(cursor_pos);
else cursor_pos = line_edit->cursorPosition();
// Get current prosition
int cur_index = line_edit->cursorPosition();
/**
* NOTE
*
* prev_delimiter_index should actually point at final white space AFTER the delimiter.
*/
// Get index of last delimiter before current position
int prev_delimiter_index = str.mid(0, cur_index).lastIndexOf(delimiter);
while(str.at(prev_delimiter_index + 1).isSpace()) prev_delimiter_index++;
// Get index of first delimiter after current position (or EOL if no delimiter after cursor)
int next_delimiter_index = str.indexOf(delimiter, cur_index);
if(next_delimiter_index == -1) {
next_delimiter_index = str.size();
}
// Get part of string that occurs before cursor
QString part1 = str.mid(0, prev_delimiter_index + 1);
// Get string value from before auto finished string is selected
QString pre = str.mid(prev_delimiter_index + 1, cur_index - prev_delimiter_index - 1);
// Get part of string that occurs AFTER cursor
QString part2 = str.mid(next_delimiter_index);
return part1 + auto_string + part2;
}
QStringList DelimitedCompleter::splitPath(const QString &path) const {
QLineEdit *line_edit = qobject_cast<QLineEdit*>(parent());
QStringList string_list;
int index = path.mid(0,line_edit->cursorPosition()).lastIndexOf(delimiter) + 1;
QString str = path.mid(index, line_edit->cursorPosition()-index).trimmed();
string_list << str;
return string_list;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DELIMITEDCOMPLETER PRIVATE METHODS *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DelimitedCompleter::connectSignals() {
connect(this, SIGNAL(activated(const QString &)), this, SLOT(onActivated(const QString &)));
connect(qobject_cast<QLineEdit*>(parent()), SIGNAL(cursorPositionChanged(int, int)),
this, SLOT(onCursorPositionChanged(int, int)));
}
void DelimitedCompleter::onActivated(const QString &text) {
cursor_pos = -1;
}
void DelimitedCompleter::onCursorPositionChanged(int old_pos, int new_pos) {
// If old_pos == cursor_pos then we are cycling through autocomplete list
// If not cycling through autocomplete list, then make sure cursor_pos is reset to -1
if(old_pos != cursor_pos) cursor_pos = -1;
}
QCompleter::setCompletionPrefix(const QString &prefix);
where prefix is just your last single word you want to completeQLineEdit
and go from there.