QScintilla 如何连续获取文本编辑小部件中的光标位置?

QScintilla how to continously get cursor position in textEdit widget?

我正在使用 C++ 开发源代码编辑器,使用 Qt5 和 QScintilla 作为框架。在这个项目中,我想连续显示文本光标的行和列(光标位置),所以我需要一个在移动文本光标时发出的信号。根据 QScintilla 文档,每当光标移动时,cursorPositionChanged(int line, int index) 都会发出想要的信号,所以我想这一定是我需要的方法吗?这是我到目前为止所做的:

// notify if cursor position changed
connect(textEdit, SIGNAL(cursorPositionChanged(int line, int index)), this, SLOT(showCurrendCursorPosition()));

我的代码可以编译,编辑器 window 会按要求显示,但不幸的是,我收到了警告:

QObject::connect: No such signal QsciScintilla::cursorPositionChanged(int line, int index)

有人可以向我提供 QScintilla C++ 或 Python 示例来说明如何连续获取和显示当前光标位置吗?

完整的源代码托管在这里: https://github.com/mbergmann-sh/qAmigaED

感谢任何提示!

问题是由运行时验证的旧连接语法引起的,此外,旧语法还有另一个问题,必须匹配签名。在您的情况下,解决方案是使用没有您提到的问题的新连接语法。

connect(textEdit, &QTextEdit::cursorPositionChanged, this, &MainWindow::showCurrendCursorPosition);

更多信息您可以查看:

谢谢,eyllanesc,你的解决方案工作正常! 我自己也找到了一个可行的解决方案,只需从连接调用中删除命名的变量即可:

// notify if cursor position changed
connect(textEdit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(showCurrendCursorPosition()));

...

//
// show current cursor position and display
// line and row in app's status bar
//
void MainWindow::showCurrendCursorPosition()
{
    int line, index;
    qDebug() << "Cursor position has changed!";
    textEdit->getCursorPosition(&line, &index);
    qDebug() << "X: " << line << ", Y: " << index;
}

此主题已解决。