QScintilla如何在textEdit小部件中持续获取光标位置?

3

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

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

我的代码编译成功,编辑器窗口也按照预期显示出来了,但不幸的是,我收到了一个警告:
QObject::connect: No such signal QsciScintilla::cursorPositionChanged(int line, int index)

请问是否有人能够提供一个QScintilla C++或Python示例,展示如何持续获取并显示当前光标位置?

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

感谢任何提示!

2个回答

2

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

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

想要更多信息,请查看以下链接:


1

谢谢,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;
}

这个主题已经解决。

我建议使用新的语法,因为其中一个重要优点是连接在编译时而不是运行时验证,这样您可以在执行程序之前看到错误。 - eyllanesc
好的,那绝对有道理。再次感谢! :) - Michael Bergmann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接