Seite 1 von 1

Position des Textcursors in Pixel

Verfasst: 19. September 2008 11:00
von hannes_c
Hallo,

ich versuche vergeblich die Position des Textcursors in einem QTextEdit
zu bestimmen.


QRect rect = textedit->cursorRect();
als auch
QTextCursor textcursor = textedit->textCursor();
QRect rect = textedit->cursorRect(textcursor);

scheitern.

Kann mir jemand weiterhelfen?

LG, Hannes

Verfasst: 19. September 2008 12:18
von upsala
Warum scheitern die? Laut Doku sollte es so funktionieren...

Verfasst: 19. September 2008 13:14
von patrik08
Ich brauche QGraphicsView um richt text seiten zu zeigen im programm

http://www.qt-apps.org/content/show.php ... tent=67552

Dort mache ich es so:

funktioniert wo keine tabellen sind & textframe! (also immer im block paragraph)

Code: Alles auswählen


QLineF TextProcessor::BlinkCursorLine()
{
	QLineF CursorDrawLine(QPointF(0,0),QPointF(0,10));  /* error line */
	/* QTextDocument *_d;  */
	const QRectF xxtmp = _d->documentLayout()->blockBoundingRect(textCursor().block());
	QTextFrame  *Tframe = _d->rootFrame();
	root_format = Tframe->frameFormat();
	QTextLine TTline = currentTextLine(textCursor());
	if ( TTline.isValid() ) {
	   int pos = textCursor().position() - textCursor().block().position();
		 const qreal TextCursorStartTop = TTline.lineNumber() * TTline.height() + xxtmp.top();
		 const qreal TextCursorStartLeft = TTline.cursorToX(pos) + root_format.leftMargin() + root_format.padding();
		 CursorDrawLine = QLineF(QPointF(TextCursorStartLeft,TextCursorStartTop),QPointF(TextCursorStartLeft,TextCursorStartTop + TTline.height()));
	}
	return CursorDrawLine;
}


QTextLine TextProcessor::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);
}



Den cursor muss ich dan noch um y mappen auf die jeweilige seiten anzahl distanz...

Verfasst: 19. September 2008 13:48
von hannes_c
@patrik
danke, ich schau es mir mal an.

@upsala,
Weil ich stets die Werte -4,2,7,11 für left, top, right, bottom, erhalte.
Lediglich wenn ich die Tab Taste drücke erhalte ich vernünftige Werte.

:?

Allerdings wüßte ich ja nur gern, an welcher koordinate (pixel) der Cursor
sich befindet, damit ich dann dies Position zum Öffnen eines Fenster verwenden kann.

Gruß, Hannes :-)

Verfasst: 19. September 2008 16:39
von hannes_c
Danke an alle


Jetzt funktionieren

Code: Alles auswählen

QTextCursor textcursor = textedit->textCursor();
QRect rect = textedit->cursorRect(textcursor);
bei mir auch.

Liegt wohl an dem Code der noch dazwischen liegt. :-)
Also muss ich nun mal die Ursache suchen.

LG, Hannes