Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to show text cursor in QTextEdit Widget, if the widget doesn't have focus.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.9k Views 1 Watching
  • 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 Offline
    P Offline
    poor_robert
    wrote on 30 Dec 2011, 14:22 last edited by
    #1

    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
    0
    • G Offline
      G Offline
      goetz
      wrote on 30 Dec 2011, 17:08 last edited by
      #2

      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
      0
      • P Offline
        P Offline
        poor_robert
        wrote on 7 Jan 2012, 13:53 last edited by
        #3

        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
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved