Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QScintilla how to continously get cursor position in textEdit widget?

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!

like image 440
Michael Bergmann Avatar asked Dec 07 '25 06:12

Michael Bergmann


2 Answers

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:

  • https://wiki.qt.io/New_Signal_Slot_Syntax
like image 125
eyllanesc Avatar answered Dec 09 '25 18:12

eyllanesc


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.

like image 40
Michael Bergmann Avatar answered Dec 09 '25 18:12

Michael Bergmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!