Reputation: 27
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
Upvotes: 1
Views: 1178
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
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