PDA

View Full Version : QTextEdit - Getting the current line/cursor position (Qt Jambi)


NOR
7th May 2008, 11:28
Hi, I'm writing a simple text editor and I'm using QTextEdit as a base
I'm trying to implement a "indent" action.
I basically just want to add four spaces at the beginning of the current line.
By "current line" I mean the line that the cursor (or whatever it's called) is at. You know... Just like in any other editor.

The problem is that I´m having trouble finding the position in the document I want. So I can get the current line.

I've been playing around with QTextCursor and such, but with no luck.
I've tried updating othe cursor on the cursorPositionChanged signal, but that only works when some content is changed. Not when i move the cursor around with the arrow keys or click somewhere else.

The JavaDoc suggests that I should use cursorPositionChanged() instead for this, but that doesn't seem to exist in Jambi.

Could anyone help me out? Point me in the right direction or suggest some kind of workaround?

Thanks for your time,
Thomas

patrik08
12th May 2008, 10:14
To find the line ...
you need QTextLine it can show you rect line nr char on cursor and X vieport position mouse and other thing....





QTextLine currentTextLine(const QTextCursor &cursor)
{
const QTextBlock block = cursor.block();
if (!block.isValid())
return QTextLine();

const QTextLayout *layout = block.layout();
if (!layout)
return QTextLine();

const int relativePos = cursor.position() - block.position();
return layout->lineForTextPosition(relativePos);
}