1

I have created simple notepad from Qt/C++. I want to print the line number on status bar of QMainWindow when I click somewhere on the text area, like notepad of Microsoft Windows

Status bar with line number

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
G.Wije
  • 27
  • 1
  • 9
  • 1
    What have you tried? You just need the current cursor position in the text edit and a way to set text on the status bar. ;) – xander Dec 19 '17 at 10:52
  • is this QPoint globalCursorPos = QCursor::pos(); valid for current cursor position ,and how can i print – G.Wije Dec 19 '17 at 11:31

3 Answers3

3

You can connect the cursorPositionChanged() signal of your text area to a custom slot of your QMainWindow:

// the connection
connect(ui->plainTextEdit, SIGNAL(cursorPositionChanged()), this, SLOT(showCursorPos()));
// your custom slot
void MainWindow::showCursorPos()
{
    int line = ui->plainTextEdit->textCursor().blockNumber()+1;
    int pos = ui->plainTextEdit->textCursor().columnNumber()+1;
    ui->statusBar->showMessage(QString("Ln %1, Col %2").arg(line).arg(pos));
}
thibsc
  • 3,747
  • 2
  • 18
  • 38
0

I'm guessing you're using QTextEdit as the "editor" widget.

To get where is the cursor in your QTextEdit, you should use

row = myTextEdit->textCursor()->blockNumber();

and for the column

column = myTextEdit->textCursor()->positionInBlock();

Then just edit your status bar text with these infos

Tzig
  • 726
  • 5
  • 20
0

use this codeui->textEdit->document()->lineCount();