Position des Textcursors in Pixel

Alles rund um die Programmierung mit Qt
Antworten
hannes_c
Beiträge: 5
Registriert: 19. September 2008 10:44

Position des Textcursors in Pixel

Beitrag 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
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Warum scheitern die? Laut Doku sollte es so funktionieren...
patrik08
Beiträge: 746
Registriert: 27. Februar 2006 10:48
Wohnort: DE Freiburg

Beitrag 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...
.........................
speack português italiano deutsch english castellà qt
hannes_c
Beiträge: 5
Registriert: 19. September 2008 10:44

Beitrag 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 :-)
hannes_c
Beiträge: 5
Registriert: 19. September 2008 10:44

Beitrag 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
Antworten