为什么插槽函数即使成功调用也无法插入文本
Why the slot function cannot insert text even if it's successfully called
我正在用QScintilla用Qt编写代码编辑器。
我想在输入前括号时自动完成后括号。这样我就可以将cursorPositionChanged(int, int)
信号连接到插槽complete_braces()
并且连接工作正常。但是即使调用了 slot 函数,insert()
语句也不起作用。
- 这适用于Windows 10 PC,运行Qt 5.13.0(MinGW 7.3.0 32位),Qt Creator 4.9.2,QScintilla 2.11.2。
- 信号和插槽的连接工作是因为
qDebug()
语句正确输出。 - 当我将工具栏按钮触发器连接到插槽功能并按下按钮时。后支架放置正确。
- 当我在普通函数中调用它时,
insert(QString)
函数会起作用。
页眉:
/* codeeditor.h */
class CodeEditor : public QsciScintilla
{
Q_OBJECT
...
public slots:
void complete_brackets();
...
};
法典:
/* 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.
}
}
...
我希望插槽函数中的insert(QString)
函数能够正确调用,但事实并非如此。
如何使插入语句生效,或者是否有其他方法可以自动填充括号?
似乎QsciScintilla
不允许在连接到textChanged
信号的插槽中添加文本,一种可能的解决方案是稍后使用QTimer::singleShot()
添加它:
void CodeEditor::complete_brackets(){
int line, index;
getCursorPosition(&line, &index);
if (text(line)[index] == '(')
QTimer::singleShot(0, [this, line, index](){
insert(")");
setCursorPosition(line, index+2);
});
}
另一方面,建议您使用新的连接语法:
connect(this, &QsciScintilla::textChanged, this, &CodeEditor::complete_brackets);
相关文章:
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- Doxygen - 如何在不生成图形的情况下生成文本调用关系结果
- 如何从代码本身向 wxwidgets 中的文本控件插入字符?
- 如何在MFC中的静态文本控件上插入图标?
- 为什么插槽函数即使成功调用也无法插入文本
- LLVM插入功能调用到另一个函数中
- 如何在LLVM传递中的模块末尾插入函数调用?
- fputc在插入文本之前将第一行留空
- 在Embarcadero Tools API中的光标位置插入文本
- 在文件中插入文本只工作一次
- C++ 在追加 Windows API 的特定位置插入文本
- 如何对表中的插入事件调用 exe
- 如何通过流插入运算符调用成员函数
- 在QTextEdit中,如何检测用户何时只将光标插入文本区域一次
- 在万向节中插入函数调用
- 在函数之间的c++代码中插入文本
- 使用std::ostream在每行之前插入文本
- VC++:在文本框中插入文本,不重复
- 无序集,值得在插入之前调用find
- 插入文本