QT-QTextedit to get the number of rows and columns

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即为行数

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325757277&siteId=291194637