The constructor for
TextEdit
constructs a
TextEdit
with a parent and initializes
c
. The instructions to use the completer is displayed on the
TextEdit
object, using the
setPlainText()
函数。
TextEdit::TextEdit(QWidget *parent)
: QTextEdit(parent)
{
setPlainText(tr("This TextEdit provides autocompletions for words that have more than"
" 3 characters. You can trigger autocompletion using ") +
QKeySequence("Ctrl+E").toString(QKeySequence::NativeText));
}
此外,
TextEdit
also includes a default destructor:
TextEdit::~TextEdit()
{
}
setCompleter()
function accepts a
completer
and sets it up. We use
if
(c)
to check if
c
has been initialized. If it has been initialized, the
disconnect()
function is invoked to disconnect the signal from the slot. This is to ensure that no previous completer object is still connected to the slot.
void TextEdit::setCompleter(QCompleter *completer)
{
if (c)
c->disconnect(this);
c = completer;
if (!c)
return;
c->setWidget(this);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setCaseSensitivity(Qt::CaseInsensitive);
QObject::connect(c, QOverload<const QString &>::of(&QCompleter::activated),
this, &TextEdit::insertCompletion);
}
We then instantiate
c
with
completer
and set it as
TextEdit
‘s widget. The completion mode and case sensitivity are also set and then we connect the
activated()
signal to the
insertCompletion()
槽。
completer()
function is a getter function that returns
c
.
QCompleter *TextEdit::completer() const
{
return c;
}
The completer pops up the options available, based on the contents of
wordlist.txt
, but the text cursor is responsible for filling in the missing characters, according to the user’s choice of word.
Suppose the user inputs “ACT” and accepts the completer’s suggestion of “ACTUAL”. The
completion
string is then sent to
insertCompletion()
by the completer’s
activated()
信号。
insertCompletion()
function is responsible for completing the word using a
QTextCursor
对象,
tc
. It validates to ensure that the completer’s widget is
TextEdit
before using
tc
to insert the extra characters to complete the word.
void TextEdit::insertCompletion(const QString &completion)
{
if (c->widget() != this)
return;
QTextCursor tc = textCursor();
int extra = completion.length() - c->completionPrefix().length();
tc.movePosition(QTextCursor::Left);
tc.movePosition(QTextCursor::EndOfWord);
tc.insertText(completion.right(extra));
setTextCursor(tc);
}
The figure below illustrates this process:
completion.length()
= 6
c->completionPrefix().length()
=3
The difference between these two values is
extra
, which is 3. This means that the last three characters from the right, “U”, “A”, and “L”, will be inserted by
tc
.
textUnderCursor()
function uses a
QTextCursor
,
tc
, to select a word under the cursor and return it.
QString TextEdit::textUnderCursor() const
{
QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
return tc.selectedText();
}
TextEdit
class reimplements
focusInEvent()
function, which is an event handler used to receive keyboard focus events for the widget.
void TextEdit::focusInEvent(QFocusEvent *e)
{
if (c)
c->setWidget(this);
QTextEdit::focusInEvent(e);
}
keyPressEvent()
is reimplemented to ignore key events like
Key_Enter
,
Key_Return
,
Key_Escape
,
Key_Tab
,和
Key_Backtab
so the completer can handle them.
If there is an active completer, we cannot process the shortcut, Ctrl+E.
void TextEdit::keyPressEvent(QKeyEvent *e)
{
if (c && c->popup()->isVisible()) {
// The following keys are forwarded by the completer to the widget
switch (e->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
e->ignore();
return; // let the completer do default behavior
default:
break;
}
}
const bool isShortcut = (e->modifiers().testFlag(Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
if (!c || !isShortcut) // do not process the shortcut when we have a completer
QTextEdit::keyPressEvent(e);
We also handle other modifiers and shortcuts for which we do not want the completer to respond to.
const bool ctrlOrShift = e->modifiers().testFlag(Qt::ControlModifier) ||
e->modifiers().testFlag(Qt::ShiftModifier);
if (!c || (ctrlOrShift && e->text().isEmpty()))
return;
static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
const bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
|| eow.contains(e->text().right(1)))) {
c->popup()->hide();
return;
}
if (completionPrefix != c->completionPrefix()) {
c->setCompletionPrefix(completionPrefix);
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
}
QRect cr = cursorRect();
cr.setWidth(c->popup()->sizeHintForColumn(0)
+ c->popup()->verticalScrollBar()->sizeHint().width());
c->complete(cr); // popup it up!
}
Finally, we pop up the completer.