
Originally Posted by
lanz
Use findBlockByLineNumber (line_number -1), since findBlockByNumber works on zero-based line numbers.
Hey, thanks for that. I wasn't aware that the findBlockByNumber() function uses 0-based line numbers, and it doesn't mention it in the docs.
I just assumed it used 1-based numbers.
I tried modifying the code in my last post to use line_number - 1 instead of just line_number, and that appears to have fixed it.
It now selects
the right line that I enter in. However, I have noticed something a bit strange in its behavior: Instead of just selecting the line that I entered, it also appears to select a
one-character-width space before the line as well (even if the line before is empty). And if I enter in a line number corresponding to a line that does not follow an empty line,
it does not select any of the characters of the previous line, but appears to select a one-character-width space after the previous line's characters. I do not know why this is.
I suspect it has something to do with Qt's concept of a "block".
After that you can set position of the cursor (rather than selecting whole block):
Thanks, but I'd rather keep selecting the whole block, reason being when the text edit is in read-only mode (which is true in my application when the text editor mode is in "Standard mode", meaning you can only edit the document with the GUI controls,
and can not directly modify the document), it does not show the cursor, and you can only select the text.
QTextBlock text_block
= central_widget_TextDocument
->findBlockByLineNumber
(line_number
-1);
QTextCursor text_cursor
= central_widget_TextEdit
->textCursor
();
text_cursor.setPosition (text_block.position ());
central_widget_TextEdit->setFocus ();
central_widget_TextEdit->setTextCursor (text_cursor);
QTextBlock text_block = central_widget_TextDocument->findBlockByLineNumber(line_number-1);
QTextCursor text_cursor = central_widget_TextEdit->textCursor ();
text_cursor.setPosition (text_block.position ());
central_widget_TextEdit->setFocus ();
central_widget_TextEdit->setTextCursor (text_cursor);
To copy to clipboard, switch view to plain text mode
Thanks for the example code. I didn't even think of looking at the QTextBlock class for a function to get the position.
However, since "text_block.position()" refers to a position corresponding to that of the first character of the block in the document, wouldn't that position be different in terms of cursor positions (since the cursor is always in between characters, and not on the same space)? Maybe it should be "text_block.position() - 1"?
BUT, I don't think i fully understand concept of a block. Can it contain multiple lines?
Can anyone clarify on that?
Good question. I'm wondering the same thing myself, especially after the results I just got.
I will wait for an answer...
If this is the case, this method will not work. Then you can simply have a list of the absolute positions of strings inside your text edit and set up cursor accordingly to this list.
To display line numbers you can subclass text edit and do custom painting in paint event using drawText and QFontMetrics (first thing that comes to mind).
What is unclear for me is how you get what lines are displayed.
Keep in mind, I'm still relatively new to Qt, and don't quite understand how to do "custom painting in paint event".
I would appreciate it if you could give some example code demonstrating this.
Thanks.
Added after 47 minutes:
Ok, I guess not (about the cursor position thing)...
I just tried the following code, and the cursor (in "Hand-Editing" mode) shows up on the right position on the right line when I enter a line to go to.
But, as before, the selection problem still exists.
void C_MainWindow::goToLineAction() {
bool ok;
int line_number
= QInputDialog::getInt(this, tr
("Go to Line"),
tr("Enter a line number to go to: "), 1, 1, central_widget_TextDocument->blockCount(), 1, &ok);
if (ok) {
/*
QTextCursor text_cursor(central_widget_TextDocument->findBlockByLineNumber(line_number - 1));
text_cursor.select(QTextCursor::BlockUnderCursor);
central_widget_TextEdit->setTextCursor(text_cursor);
*/
QTextBlock text_block
= central_widget_TextDocument
->findBlockByLineNumber
(line_number
- 1);
QTextCursor text_cursor
= central_widget_TextEdit
->textCursor
();
text_cursor.setPosition (text_block.position());
central_widget_TextEdit->setFocus();
central_widget_TextEdit->setTextCursor (text_cursor);
}
}
void C_MainWindow::goToLineAction() {
bool ok;
int line_number = QInputDialog::getInt(this, tr("Go to Line"),
tr("Enter a line number to go to: "), 1, 1, central_widget_TextDocument->blockCount(), 1, &ok);
if (ok) {
/*
QTextCursor text_cursor(central_widget_TextDocument->findBlockByLineNumber(line_number - 1));
text_cursor.select(QTextCursor::BlockUnderCursor);
central_widget_TextEdit->setTextCursor(text_cursor);
*/
QTextBlock text_block = central_widget_TextDocument->findBlockByLineNumber(line_number - 1);
QTextCursor text_cursor = central_widget_TextEdit->textCursor ();
text_cursor.setPosition (text_block.position());
central_widget_TextEdit->setFocus();
text_cursor.select(QTextCursor::BlockUnderCursor);
central_widget_TextEdit->setTextCursor (text_cursor);
}
}
To copy to clipboard, switch view to plain text mode
EDIT:
Nevemind about the selection problem. FIXED! 
void C_MainWindow::goToLineAction() {
bool ok;
int line_number
= QInputDialog::getInt(this, tr
("Go to Line"),
tr("Enter a line number to go to: "), 1, 1, central_widget_TextDocument->blockCount(), 1, &ok);
if (ok) {
QTextCursor text_cursor
(central_widget_TextDocument
->findBlockByLineNumber
(line_number
- 1));
central_widget_TextEdit->setTextCursor(text_cursor);
/*
QTextBlock text_block = central_widget_TextDocument->findBlockByLineNumber(line_number - 1);
QTextCursor text_cursor = central_widget_TextEdit->textCursor ();
text_cursor.setPosition (text_block.position());
central_widget_TextEdit->setFocus();
text_cursor.select(QTextCursor::LineUnderCursor);
central_widget_TextEdit->setTextCursor(text_cursor);
*/
}
}
void C_MainWindow::goToLineAction() {
bool ok;
int line_number = QInputDialog::getInt(this, tr("Go to Line"),
tr("Enter a line number to go to: "), 1, 1, central_widget_TextDocument->blockCount(), 1, &ok);
if (ok) {
QTextCursor text_cursor(central_widget_TextDocument->findBlockByLineNumber(line_number - 1));
text_cursor.select(QTextCursor::LineUnderCursor);
central_widget_TextEdit->setTextCursor(text_cursor);
/*
QTextBlock text_block = central_widget_TextDocument->findBlockByLineNumber(line_number - 1);
QTextCursor text_cursor = central_widget_TextEdit->textCursor ();
text_cursor.setPosition (text_block.position());
central_widget_TextEdit->setFocus();
text_cursor.select(QTextCursor::LineUnderCursor);
central_widget_TextEdit->setTextCursor(text_cursor);
*/
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks