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.
Robert.
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.
2 replies
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:
header:
- #include <QTextEdit>
- class QPainter;
- class QTimer;
- {
- Q_OBJECT
- public:
- ~editor();
- public slots:
- void slot_BlinkCursor( );
- protected:
- private:
- bool bIsCursorVisible;
- };
source file:
- #include <QPainter>
- #include <QTimer>
- #include <QDebug>
- #include "editor.h"
- {
- bIsCursorVisible = true;
- CursorTimer->setInterval( 500 );
- connect( CursorTimer, SIGNAL( timeout() ), this, SLOT( slot_BlinkCursor() ));
- CursorTimer->start();
- }
- editor::~editor()
- {
- //nothing
- }
- void editor::slot_BlinkCursor( ) {
- if( bIsCursorVisible )
- {
- bIsCursorVisible = false;
- }
- else
- {
- bIsCursorVisible = true;
- }
- viewport()->update();
- }
- {
- if( bIsCursorVisible )
- {
- r.setWidth(1);
- }
- }
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.
Rob
You must log in to post a reply. Not a member yet? Register here!


