Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, October 2007
locate/position/indicate cursor's line and column?


Message 1 in thread

Dear Users of QTextEdit, QTextBrowser, or XML players,

When a DOM parser fails to setContent it tells an errorLine and an 
errorColumn. I wish to locate them by putting a cursor right on the spot.

Qt4 is awkward to do cursor positioning. QTextEdit has a method 
setTextCursor. Problem is there is no method in QTextCursor to position 
a cursor at a given line and column, instead, it takes a position 
refering to the beginning of the document.

Should I count each line-break and calculate the position?
int lineCount = 0, lastPosition = 0, index;
forever{
    index = m_content.indexOf("\n", lastPosition);
    if(index == -1) break;
    lineCount ++; if(lineCount == errorLine) break;
    lastPosition = index + 1;
};
If(index != -1)
{
    int pos = index + errorColumn;
    cursor.setPosition(pos, QTextCursor::MoveAnchor);
    textEdit->setTextCursor(cursor);
}
This sounds awkward in Qt4.
Is there a better way to locate/position/indicate cursor's line and column?

Thanks,
Lingfa



--
 [ signature omitted ]