First make a statement:
note that myTextEdit here is our QTextedit
QTextCursor tc = myTextEdit->textCursor();
QTextLayout *pLayout = tc.block().layout();
Get the cursor's position on a line:
int nCurpos = tc.position() - tc.block().position();
//这里的nCursor其实就是我们一个文本在一行中的位置
qDebug() <<"nCurpos " << nCurpos<<endl;
Get the line number where the cursor is located
int nTextline = pLayout->lineForTextPosition(nCurpos).lineNumber() + tc.block().firstLineNumber();
qDebug()<<nTextline<<endl;
//nTextline即为行数