// completertextedit.cpp #include "completertextedit.h" #include #include #include #include #include #include CompleterTextEdit::CompleterTextEdit(QWidget *parent) : QTextEdit(parent), m_completer(NULL) { } QCompleter * CompleterTextEdit::GetCompleter() { return m_completer; } void CompleterTextEdit::setCompleter(QCompleter *completer) { m_completer = completer; if (m_completer) { m_completer->setWidget(this); connect(m_completer, SIGNAL(activated(QString)), this, SLOT(onCompleterActivated(QString))); } } void CompleterTextEdit::keyReleaseEvent(QKeyEvent *e) { QTextEdit::keyReleaseEvent(e); if(e->key()==Qt::Key_Comma) { // QCoreApplication::postEvent(this, new QKeyEvent(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, 0)); return; }; } void CompleterTextEdit::mousePressEvent(QMouseEvent *e) { QTextCursor cursor = textCursor(); QString text1=cursor.selectedText(); cursor.clearSelection(); if (e->button() == Qt::MiddleButton) { // cursor.setPosition(e->pos()); QMouseEvent ss(e->type(),e->pos(),Qt::LeftButton,e->buttons(),e->modifiers()); QTextEdit::mousePressEvent(&ss); QTextEdit::mouseReleaseEvent(&ss); setTextCursor(textCursor()); if(text1.lastIndexOf(0x2029)>=0) { text1.remove(0x2029); } textCursor().insertText(text1); } else QTextEdit::mousePressEvent(e); } void CompleterTextEdit::keyPressEvent(QKeyEvent *e) { if (m_completer) { if (m_completer->popup()->isVisible()) { switch(e->key()) { case Qt::Key_Escape: case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Tab: e->ignore(); return; default: break; } } QTextEdit::keyPressEvent(e); if(e->key()==Qt::Key_Comma) { this->insertPlainText(" "); } if(e->key()!=Qt::Key_Comma&&e->key()!=Qt::Key_Space &&e->key()!=Qt::Key_Up &&e->key()!=Qt::Key_Down &&e->key()!=Qt::Key_Left &&e->key()!=Qt::Key_Right ) { QString completerPrefix = this->wordUnderCursor(); m_completer->setCompletionPrefix(completerPrefix); // 通过设置QCompleter的前缀,来让Completer寻找关键词 m_completer->complete(); } } else { QTextEdit::keyPressEvent(e); } } void CompleterTextEdit::onCompleterActivated(const QString &completion) { QString completionPrefix = wordUnderCursor(), shouldInertText = completion; QTextCursor cursor = this->textCursor(); if (!completion.contains(completionPrefix)) {// delete the previously typed. cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, completionPrefix.size()); cursor.clearSelection(); } else { // 补全相应的字符 shouldInertText = shouldInertText.replace( shouldInertText.indexOf(completionPrefix), completionPrefix.size(), ""); } cursor.insertText(shouldInertText); } QString CompleterTextEdit::wordUnderCursor() { //不断向左移动cursor,并选中字符,并查看选中的单词中是否含有空格——空格作为单词的分隔符 QTextCursor cursor = this->textCursor(); int pos1=cursor.position(); while (cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor)) { int pos2=cursor.position(); if(pos2-pos1>1) { break; } pos1=pos2; if (cursor.selectedText().contains(" ")||cursor.selectedText().contains("\n")||cursor.selectedText().contains(",")||cursor.selectedText().contains("\r")) { break; } } QString cs=cursor.selectedText(); cs=cs.remove(" "); cs=cs.remove(","); cs=cs.remove("\r"); cs=cs.remove("\n"); return cs; }