Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    3
    5485
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      poor_robert last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • G
        goetz last edited by

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

        @
        button->setFocusPolicy(Qt::NoFocus);
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • P
          poor_robert last edited by

          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;

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

          public slots:
          void slot_BlinkCursor( );

          protected:
          void paintEvent( QPaintEvent* pEvent );

          private:
          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() ));

          CursorTimer->start();
          }

          editor::~editor()
          {
          //nothing
          }

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

          viewport()->update();
          

          }

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

          if( bIsCursorVisible )
          {
          QRect r = cursorRect();
          r.setWidth(1);
          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.

          Rob

          1 Reply Last reply Reply Quote 0
          • First post
            Last post