QLineEdit::textEdited() equivalent in QTextEdit?
Asked Answered
B

2

5

In QLineEdit, there is a textEdit() signal, which only emits if the user changes the text, but not when you call setText(),

So what's the equivalent in QTextEdit? I only see a textChanged() signal, and the documentation states it is emitted whenever the text document changes.

EDIT

I want to implement an auto-save feature, with a QTimer of course,

So when you start editing the document, the timer starts, and when timed out, I save the text inside the widget.

Bounteous answered 26/1, 2013 at 8:33 Comment(1)
I don't think there's a signal for that. What scenario is this for?Raddy
Z
12

You can block signals of the QTextEdit widget whenever you insert/modify the contents yourself, and then release the block when you're done. By doing that the signal will only be emitted when the user makes changes to the contents.

bool QObject::blockSignals(bool block)
Zygapophysis answered 26/1, 2013 at 14:59 Comment(0)
M
0

I looked for my mentor's code and he solved the problem this way:

  1. Install event filter for QTextEdit object (this is pointer to mEdit holder, which is QWidget)
mEdit->installEventFilter(this);
  1. Override QObject::eventFilter method in class, which contains QTextEdit object (remember, it's QWidget inheritor)
//override
bool CustomEditWidget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == mEdit && event->type() == QEvent::FocusOut){
        changeValue(mEdit->toPlainText());
    }
    return false;
}

The rest you can see in the documentation, there are examples: https://doc.qt.io/qt-6/qobject.html#eventFilter

Mollescent answered 14/3, 2023 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.