TyR wrote:
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.

If I understand correct you just want an action that indents the current line, right? I've implemented a small example that illustrates how this is done.

The position is always available via the QTextEdit's textCursor(), so when the action is triggered, TAB in this case, I:
 - get the cursor
 - store the position
 - move to beginning of the line
 - insert four spaces
 - move back to where I was

Is this what you were looking for?

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

I don't understand exactly what you mean here... The cursorPositionChanged signal is availble in the QTextEdit docs as far as I can see.

best regards,
Gunnar
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

public class TextEditWithIndent extends QTextEdit {

    protected void keyPressEvent(QKeyEvent e) {
        if (e.key() == Qt.Key.Key_Tab.value()) {

            QTextCursor cursor = textCursor();
            int pos = cursor.position();

            // Move to beginning of line and insert spaces
            cursor.movePosition(QTextCursor.MoveOperation.StartOfLine);
            cursor.insertText("    ");

            // Move back, and compensate for inserted text...
            cursor.setPosition(pos + 4);

            return;
        }
        super.keyPressEvent(e);
    }

    public static void main(String args[]) {

        QApplication.initialize(args);

        QTextEdit edit = new TextEditWithIndent();
        edit.show();



        QApplication.exec();

    }

}
_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to