如何改变QCompleter弹出列表的位置?
How to change the position of the pop-up list of a QCompleter?
我正在开发基于 Qt5 的代码编辑器。当我尝试使用 QCompleter 向我的编辑器添加自动完成功能时,我发现弹出列表总是出现在编辑区域的底部。如何让它像真实的一样在光标位置弹出IDE?
这是定义完成器的代码:
QCompleter* HintList = new QCompleter(EditArea);
// EditArea is a QPlainTextEdit item
HintList->setFilterMode(Qt::MatchStartsWith);
HintList->setCompletionMode(QCompleter::PopupCompletion);
QStringListModel* KeyList = new QStringListModel(keywords, this);
// keywords is a QStringList item
HintList->setModel(KeyList);
EditArea->setCompleter(HintList);
您可以通过子类化您的编辑器来实现它。这是 QLineEdit
的示例
class ExtendedLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit ExtendedLineEdit(QWidget *parent = nullptr);
void setWordCompleter(QCompleter* c);
protected:
void keyPressEvent(QKeyEvent *e);
private slots:
void insertCompletionWord(const QString& txtComp);
private:
QCompleter* m_completerWord;
void showCustomCompleter(QCompleter* completer);
};
void ExtendedLineEdit::setWordCompleter(QCompleter *c)
{
m_completerWord = c;
m_completerWord->setWidget(this);
connect(m_completerWord, SIGNAL(activated(QString)),
this, SLOT(insertCompletionWord(QString)));
}
void ExtendedLineEdit::keyPressEvent(QKeyEvent *e)
{
QLineEdit::keyPressEvent(e);
if (!m_completerWord)
return;
m_completerWord->setCompletionPrefix(this->text());
showCustomCompleter(m_completerWord);
}
void ExtendedLineEdit::insertCompletionWord(const QString &txtComp)
{
setText(txtComp);
}
void ExtendedLineEdit::showCustomCompleter(QCompleter *completer)
{
if (completer->completionPrefix().length() < 1)
{
completer->popup()->hide();
return;
}
//HERE is calculated geometry of completer popup
QRect cr = cursorRect();
cr.setWidth(completer->popup()->sizeHintForColumn(0) + completer->popup()->verticalScrollBar()->sizeHint().width());
completer->complete(cr);
}
我正在开发基于 Qt5 的代码编辑器。当我尝试使用 QCompleter 向我的编辑器添加自动完成功能时,我发现弹出列表总是出现在编辑区域的底部。如何让它像真实的一样在光标位置弹出IDE?
这是定义完成器的代码:
QCompleter* HintList = new QCompleter(EditArea);
// EditArea is a QPlainTextEdit item
HintList->setFilterMode(Qt::MatchStartsWith);
HintList->setCompletionMode(QCompleter::PopupCompletion);
QStringListModel* KeyList = new QStringListModel(keywords, this);
// keywords is a QStringList item
HintList->setModel(KeyList);
EditArea->setCompleter(HintList);
您可以通过子类化您的编辑器来实现它。这是 QLineEdit
class ExtendedLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit ExtendedLineEdit(QWidget *parent = nullptr);
void setWordCompleter(QCompleter* c);
protected:
void keyPressEvent(QKeyEvent *e);
private slots:
void insertCompletionWord(const QString& txtComp);
private:
QCompleter* m_completerWord;
void showCustomCompleter(QCompleter* completer);
};
void ExtendedLineEdit::setWordCompleter(QCompleter *c)
{
m_completerWord = c;
m_completerWord->setWidget(this);
connect(m_completerWord, SIGNAL(activated(QString)),
this, SLOT(insertCompletionWord(QString)));
}
void ExtendedLineEdit::keyPressEvent(QKeyEvent *e)
{
QLineEdit::keyPressEvent(e);
if (!m_completerWord)
return;
m_completerWord->setCompletionPrefix(this->text());
showCustomCompleter(m_completerWord);
}
void ExtendedLineEdit::insertCompletionWord(const QString &txtComp)
{
setText(txtComp);
}
void ExtendedLineEdit::showCustomCompleter(QCompleter *completer)
{
if (completer->completionPrefix().length() < 1)
{
completer->popup()->hide();
return;
}
//HERE is calculated geometry of completer popup
QRect cr = cursorRect();
cr.setWidth(completer->popup()->sizeHintForColumn(0) + completer->popup()->verticalScrollBar()->sizeHint().width());
completer->complete(cr);
}