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



我正在C++中开发一个源代码编辑器,使用Qt5和QScintilla作为框架。在这个项目中,我想连续显示文本光标的行和列(光标位置(,所以我需要一个SIGNAL,每当文本光标移动时就会出现。根据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

谢谢你的提示!

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

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

有关更多信息,您可以查看:

  • https://wiki.qt.io/New_Signal_Slot_Syntax

谢谢,eyllanesc,您的解决方案运行良好!我自己也找到了一个可行的解决方案,只需从连接调用中删除命名的vars:

// 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;
}

本主题已解决。

最新更新