Important: Please read the Qt Code of Conduct -

How to show text cursor in QTextEdit Widget, if the widget doesn't have focus.

  • Hello Qt friends!

    I have very strange problem and I hope I will find help or useful some hints here, because I can't figure it out by myself.

    I have widget which consist of text editor which is QPlainTextEdit and my own virtual keyboard. Virtual Keyboard is built from QPushButton objects. My problem is that I would like to see blinking text cursor in editor widget while i type letters using my virtual keyboard. Unfortunately cursor disappears when I click any of keyboard's buttons, because editor widget loses it's focus.

    I tried to set focusProxy feature but it didn't help. I thought about painting my own cursor, but maybe there is simplier solution, which I missed??

    If You have any ideas, please write.

    PS: I can't share source code with You, because it's company's property, but I will write some lines when I come back home
    to explain my problem and test possible solutions.

  • You could try to set focus policy to "none" on your buttons:


  • Hello again. I had some network access problem, so I couldn't answer earlier.

    Thank You Volker for Your answer, but it wasn't what I was looking for. In my project it is important to keep focus on button, because what we do is editor with on-screen (virtual) keyboard and it's on embedded device.

    I have solved this problem with subclassing QTextEdit and pain QTextCursor by myself. Here's how I did it:

    @#include <QTextEdit>

    class QPainter;
    class QTimer;

    class editor : public QTextEdit
    editor( QWidget* parent = 0);

    public slots:
    void slot_BlinkCursor( );

    void paintEvent( QPaintEvent* pEvent );

    bool bIsCursorVisible;
    QTimer* CursorTimer;

    source file:
    @#include <QPainter>
    #include <QTimer>
    #include <QDebug>
    #include "editor.h"

    editor::editor( QWidget* parent )
    : QTextEdit( parent )
    bIsCursorVisible = true;
    CursorTimer = new QTimer( this );
    CursorTimer->setInterval( 500 );
    connect( CursorTimer, SIGNAL( timeout() ), this, SLOT( slot_BlinkCursor() ));



    void editor::slot_BlinkCursor( ) {
    if( bIsCursorVisible )
    bIsCursorVisible = false;
    bIsCursorVisible = true;



    void editor::paintEvent( QPaintEvent* pEvent )
    QPainter oPainter( viewport() );

    if( bIsCursorVisible )
    QRect r = cursorRect();
    oPainter.fillRect( r, Qt::SolidPattern );
    QTextEdit::paintEvent( pEvent );


    Cursor blinks and that's very good, but I have some other problems with this code. Unfortunetally I can't reproduce them from home. Maybe when I compile this with qt-embedded version I will be able to show them.


Log in to reply