c++qtqmainwindowqstatusbar

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


Solution

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