0

I have a QTextEdit box that displays text that is clickable. After clicking the text I want the cursor to move to the line after the last line of text so that new text can be displayed without interfering with what already exists. My problem is that new text is displayed from the position I clicked the text from instead of moving to the next empty line. Here are the relevant classes.

void Dialog::handleButton()
{
    QString input= lineEdit->text();
    lineEdit->clear();
    //statValue->setText("0");

    smallEditor->append("\n");
    smallEditor->insertPlainText(input);

    bigEditor->append("\n");
    bigEditor->insertPlainText(input);

    std::string inp = input.toStdString();
    //std::cout<<inp<<std::endl;

}

void Dialog::createFormGroupBox()
{
    formGroupBox = new QGroupBox(tr("History"));
    QFormLayout *layout = new QFormLayout;

    bigEditor = new QTextEdit();
    bigEditor->setReadOnly(true);

    connect(bigEditor, SIGNAL(cursorPositionChanged()), this, SLOT(on_textEdit_clicked()));

    layout->addWidget(bigEditor);
    formGroupBox->setLayout(layout);
}

void Dialog::on_textEdit_clicked()
{
    QTextCursor cursor;
    QString text;

    cursor = bigEditor->textCursor();
    cursor.movePosition(QTextCursor::StartOfBlock);
    cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
    text = cursor.selectedText();

    std::string txt = text.toStdString();
    std::cout<<txt<<std::endl;

}

I tried using both KeepAnchor and MoveAnchor but neither made a difference.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Marco Aiello
  • 179
  • 10
  • Does this answer your question? [Problem in Moving QTextCursor to the End](https://stackoverflow.com/questions/6786641/problem-in-moving-qtextcursor-to-the-end) – musicamante Sep 29 '21 at 13:35
  • If you need to move the edit cursor of the control ("caret"), moving the QTextCursor position is not enough, and you need to set it back to the control using [`setTextCursor()`](https://doc.qt.io/qt-5/qtextedit.html#setTextCursor). – musicamante Sep 29 '21 at 13:36

0 Answers0