[SOLVED] Cursor behavior in QTextEdit after applying stylesheet

  • Dear all,

    I used FocusInEvent and FocusOutEvent to change background color of QTextEdit using stylesheet.

    If I don't change the background color, the cursor behaves correctly (appear in QTextEdit when I click it, and gone when it lost focus).

    However, when I change background color (using stylesheet), the cursor remain appear (and blinking) in QTextEdit, so that when I click different QTextEdit, the cursor will appear (and blinking) in two QTextEdit.

    I tried to set cursor width to 0 to make it disappear after QTextEdit lost focus, however I couldn't restore back the cursor. I set the width to 1 and 2, none of them work/recover the cursor.

    What should I do to solve/work around this issue?

    Thank you so much.

  • I did not know the solution, but I had an idea what might be wrong.

    Pass events to QTextEdit class for "further processing"

    @void TextEdit::focusInEvent(QFocusEvent *e)
    setStyleSheet("background-color: #F00");

    QTextEdit::focusInEvent(e); // Notice

    void TextEdit::focusOutEvent(QFocusEvent *e)

    QTextEdit::focusOutEvent(e); // Notice

    In my test project worked.


  • Is "TextEdit" a derived class from QTextEdit?

    I did it in different way, by overloading the eventFilter(QObject*, QEvent).

    I will try your way and I will post an update.

    Thanks a lot Jake.

  • Yes, TextEdit is derived from QTextEdit.

    If you used the eventFilter(), the procedure should be the same.
    Just forward the event at the end.
    bool TextEdit::eventFilter(QObject *obj, QEvent *event)
    // Your code

      return QTextEdit::eventFilter(obj, event);


  • Hi Jake,

    I tried using eventFilter() and pass it to QTextEdit class...didn't work.

    But the solution you gave work! Overloading focusInEvent() and focusOutEvent().

    Thanks a lot Jake!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.