I'm writing a code editor in Qt with QScintilla.
I want to automatically complete the back brackets when I enter the front brackets. So that I connect the cursorPositionChanged(int, int)
signal to complete_braces()
slot and the connection works. But the insert()
statement doesn't work even if the slot function is called.
- This is for a Windows 10 PC, running Qt 5.13.0(MinGW 7.3.0 32-bit), Qt Creator 4.9.2, QScintilla 2.11.2.
- The connection of signal and slot works because the
qDebug()
statement output correctly. - When I connect a toolbar button trigger to the slot function and push the button. The back brace is placed correctly.
- The
insert(QString)
function works when I call it in normal functions.
Header:
/* codeeditor.h */
class CodeEditor : public QsciScintilla
{
Q_OBJECT
...
public slots:
void complete_brackets();
...
};
Code:
/* codeeditor.cpp */
CodeEditor::CodeEditor()
{
...
// Slots
connect(this, SIGNAL(textChanged()),
this, SLOT(complete_brackets()));
...
}
...
void CodeEditor::complete_brackets()
{
int line, index;
getCursorPosition(&line, &index);
if (text(line)[index] == '(')
{
qDebug() << "Get a bracket"; // This statement works.
insert(QString(")")); // This statement doesn't work.
}
}
...
I expected the insert(QString)
function in the slot function to be called correctly, but it doesn't.
How can I do to make the insert statement effective or, is there any other method to auto complete the brackets?