Removing last line from QTextEdit
c++, qt
Solution
Try this (Tested):
ui->textEdit_2->setFocus();
QTextCursor storeCursorPos = ui->textEdit_2->textCursor();
ui->textEdit_2->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
ui->textEdit_2->moveCursor(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
ui->textEdit_2->moveCursor(QTextCursor::End, QTextCursor::KeepAnchor);
ui->textEdit_2->textCursor().removeSelectedText();
ui->textEdit_2->textCursor().deletePreviousChar();
ui->textEdit_2->setTextCursor(storeCursorPos);
Problem
I'm using this bit of code to try to remove the last line from a QTextEdit: ``` ui->textEdit_2->textCursor().setPosition( QTextCursor::End); auto k = ui->textEdit_2->textCursor().currentTable(); k->removeRows(k->rows() - 1, 1); ``` but I get a segmentation fault. After debugging I found that that k is null when removeRows is called. Am I doing something wrong? if yes, how can it be fixed?