In my textbrowser, I've already implemented the mousePress and found the line number when clicked. Now I want to highlight where I clicked i.e., change its background color. I knew line is different with block. Luckily in my text one line is one block. So What I did is to manipulate the block format by cursor, listed as follows:
QTextCursor cur = mytextBrowser->textCursor();
QBlockFormat f;
f.setBackground(Qt::red);
cur.selection(QTextCursor::BlockUnderCursor);
cur.setBlockFormat(f);
cur.setPosition(startPos);//I calculate this startPos before. It's where the cursor should be
mytextBrowser->setTextCursor(cur);
However, the result is odd. When I first click the text, nothing happened, sometimes maybe selected a word. Then I click again, that previous line and the above line would be highlighted. I don't understand why this happened. Could anyone give me some solutions? Thanks.