Your code doesn't even compile. It uses QBlockFormat
class that doesn't exist and cur.selection
with invalid argument. Did you just type it out of your head? Anyway, why don't you use LineUnderCursor
instead? The following code works fine for me:
void MainWindow::on_textBrowser_cursorPositionChanged() {
QTextCursor cur = ui->textBrowser->textCursor();
QTextBlockFormat f;
f.setBackground(Qt::red);
cur.select(QTextCursor::LineUnderCursor);
cur.setBlockFormat(f);
ui->textBrowser->setTextCursor(cur);
}