QTextEdit - get selection line numbers

c++, qt, qt4, qtextedit, text

Solution

It is possible, that it isn't the best solution, but it seems to work for me. The variable `selectedLines` will contain, how many lines are selected.

QTextCursor cursor = ui->plainTextEdit->textCursor();
int selectedLines = 0; //<--- this is it 
if(!cursor.selection().isEmpty())
{
    QString str = cursor.selection().toPlainText();
    selectedLines = str.count("\n")+1;
}

I hope, that it will be helpful :)

Problem

I have the following code (implemented in the mouseReleaseEvent) to detect when the user has selected lines of text: ``` QTextCursor cursor = this->textCursor(); int start = cursor.selectionStart(); int end = cursor.selectionEnd(); if(!cursor.hasSelection()) return; // No selection available qWarning() << "start: " << start << " end: " << end << endl; ``` the problem is: I need the line numbers where the selection begins and ends. I've been struggling with blocks and solved nothing, can you please give me a clue?

Original source