G.Wije
G.Wije

Reputation: 27

Qt simple note pad, print line number on status bar of QmainWindow

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

Upvotes: 1

Views: 1178

Answers (3)

user18462006
user18462006

Reputation: 1

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

Upvotes: 0

thibsc
thibsc

Reputation: 4079

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));
}

Upvotes: 3

Tzig
Tzig

Reputation: 851

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

Upvotes: 0

Related Questions