3

I'm working on a source code editor in C++, using Qt5 and QScintilla as framework. In this project, I want to continously show the row and column of the text cursor (cursor position), so I need a SIGNAL that emmits whenever the text cursor is moved. According to QScintilla docs, cursorPositionChanged(int line, int index) emmits the wanted signal whenever the cursor is moved, so I guess this must be the method that I need? This is what I did so far:

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

my code compiles and the editor window shows up as wanted, but unfortunately, I got the warning:

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

Can someone please provide me with a QScintilla C++ or Python example showing how to continously get and display the current cursor position?

Complete source code is hosted here: https://github.com/mbergmann-sh/qAmigaED

Thanks for any hints!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

2

The problem is caused by the old syntax of connection that is verified in runtime, in addition that old syntax has as another problem that have to match the signatures. In your case the solution is to use the new connection syntax that does not have the problems you mention.

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

For more information you can check:

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
1

Thanks, eyllanesc, your solution works fine! I also found a working solution myself, just had to remove the named vars from the connection call:

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

This topic is SOLVED.

  • I recommend using the new syntax as an important advantage is that the connection is verified in compile time and not in runtime, so you can see the errors before executing your program. – eyllanesc Dec 16 '18 at 06:23
  • Well, that absolutely makes sense. Thanks again! :) – Michael Bergmann Dec 16 '18 at 09:43