[Qt-interest] QTextEdit character offset

Stephen Jackson spjackson42 at gmail.com
Sat Mar 14 21:06:46 CET 2009


On Sat, Mar 14, 2009 at 3:07 PM, Keshava Krishna wrote:
>
> I have been searching for function that can get me the starting and ending
> offsets for 2 days now...
> please help me people, I am in real trouble...
>
>>
>> Hi,
>>
>> 'm building a text editor using QTextEdit, the user can scroll through the
>> text document by using vertical scrollbar.
>> i want to konw the offsets (character count) of the 1st and the last
>> character that is currently visible in the Text Area. (even though the
>> cursor position is not changed),
>> Please suggest me a way to do it.
>>
>> Regards,
>> Keshava.
>
>

The following code is not perfect but it should point you in the right
direction.

For example, add the following function to the example in
examples/assistant/simpletextviewer, plus a mechanism for calling it.

void MainWindow::showPosition()
{
    QTextCursor cursor;
    int position;
    QString line;

    cursor = textViewer->cursorForPosition ( QPoint(0,0)  );
    position = cursor.position();
    cursor.select(QTextCursor::LineUnderCursor);
    line =  QString::number(position) + QString(", ") + cursor.selectedText();

    QMessageBox::information(this, tr("Simple Text Viewer Start Position"),
                            line);

    // ----------------------------

    cursor = textViewer->cursorForPosition (
QPoint(textViewer->width(), ,textViewer->height())  );
    position = cursor.position();
    cursor.select(QTextCursor::LineUnderCursor);
    line =  QString::number(position) + QString(", ") + cursor.selectedText();

    QMessageBox::information(this, tr("Simple Text Viewer End Position"),
                            line);

}


Hope this helps,

Stephen Jackson



More information about the Qt-interest-old mailing list