December 30, 2011

poor_robert poor_robert
Lab Rat
22 posts

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

December 30, 2011

Volker Volker
Robot Herder
5428 posts

You could try to set focus policy to “none” on your buttons:

  1. button->setFocusPolicy(Qt::NoFocus);

January 7, 2012

poor_robert poor_robert
Lab Rat
22 posts

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:

  1. #include <QTextEdit>
  2.  
  3. class QPainter;
  4. class QTimer;
  5.  
  6. class editor : public QTextEdit
  7. {
  8.     Q_OBJECT
  9. public:
  10.     editor( QWidget* parent = 0);
  11.     ~editor();
  12.    
  13. public slots:
  14.     void slot_BlinkCursor( );
  15.    
  16. protected:
  17.     void paintEvent( QPaintEvent* pEvent );
  18.    
  19. private:
  20.     bool bIsCursorVisible;
  21.     QTimer* CursorTimer;
  22. };

source file:

  1. #include <QPainter>
  2. #include <QTimer>
  3. #include <QDebug>
  4. #include "editor.h"
  5.  
  6. editor::editor( QWidget* parent )
  7.  : QTextEdit( parent )
  8. {
  9.  bIsCursorVisible = true;
  10.  CursorTimer = new QTimer( this );
  11.  CursorTimer->setInterval( 500 );
  12.  connect( CursorTimer, SIGNAL( timeout() ), this, SLOT( slot_BlinkCursor() ));
  13.  
  14.  CursorTimer->start();
  15. }
  16.  
  17. editor::~editor()
  18. {
  19.  //nothing
  20. }
  21.  
  22. void editor::slot_BlinkCursor( ) {
  23.     if( bIsCursorVisible )
  24.     {
  25.     bIsCursorVisible = false;
  26.     }
  27.     else
  28.     {
  29.     bIsCursorVisible = true;
  30.     }
  31.  
  32.     viewport()->update();
  33. }
  34.  
  35.  
  36.  
  37. void editor::paintEvent( QPaintEvent* pEvent )
  38. {
  39.     QPainter oPainter( viewport() );
  40.  
  41.     if( bIsCursorVisible )
  42.     {
  43.     QRect r = cursorRect();
  44.     r.setWidth(1);
  45.     oPainter.fillRect( r, Qt::SolidPattern );
  46.     }
  47.     QTextEdit::paintEvent( pEvent );
  48. }

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

 
  ‹‹ How to get all IP address on a LAN ?      How to add a external library on Windows ››

You must log in to post a reply. Not a member yet? Register here!