Results 1 to 11 of 11

Thread: QLineEdit keep cursor active.

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QLineEdit keep cursor active.

    Hey Guys,

    I want the "QLineEdit cursor" to stay active no matter if QLineEdit has focus or not.

    How would you guys do that ?

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit keep cursor active.

    Reimplement focusOutEvent() to ignore that event and post QFocusEvent with type QEvent::FocusIn in your constructor to let the widget believe it has the focus. This way the widget will never know it doesn't have the focus.

  3. The following user says thank you to victor.fernandez for this useful post:

    bunjee (2nd September 2009)

  4. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QLineEdit keep cursor active.

    Thanks.

    Qt Code:
    1. lineEdit->installEventFilter(this);
    2.  
    3. bool qkWebShell::eventFilter(QObject * object, QEvent * event)
    4. {
    5. if (event->type() == QEvent::FocusOut) return true;
    6. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: This screws the QWidget focus policy.

  5. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit keep cursor active.

    Of course it screws the focus policy for the widget because the idea is not to let the widget know it has lost the focus.

    If you do it using event filters, don't forget to call your parent class' eventFilter() method to pass the event to it, in case it wanted to filter something:

    Qt Code:
    1. bool qkWebShell::eventFilter(QObject * object, QEvent * event)
    2. {
    3. if (event->type() == QEvent::FocusOut) return true;
    4.  
    5. return this->QLineEdit::eventFilter(object, event);
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit keep cursor active.

    Obviously your code does something completely different than the solution suggested. You are just ignoring all focus out events which is incorrect. If this is to work at all, you have to handle the event but immediately send another one to fool the widget.

    But it's probably better to subclass the line edit and change the drawing routine to convince QStyle that the widget has focus and the cursor should be drawn.

    Edit: Actually QTextLine not QStyle.
    Last edited by wysota; 2nd September 2009 at 12:05.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    bunjee (2nd September 2009)

  8. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit keep cursor active.

    I agree. It would be better to subclass QLineEdit and reimplement paintEvent() but also more difficult. If you do so, take a look at the source code of qlineedit.cpp. At the end of paintEvent() you have:

    Qt Code:
    1. if (d->cursorVisible && !d->readOnly && !d->hideCursor)
    2. d->textLayout.drawCursor(&p, topLeft, cursor, style()->pixelMetric(QStyle::PM_TextCursorWidth));
    To copy to clipboard, switch view to plain text mode 

    You would remove d->cursorVisible from the if(). Keep in mind you can't access the private object so you may either find another way to get that information or copy its implementation an rename it.

  9. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QLineEdit keep cursor active.

    Quote Originally Posted by victor.fernandez View Post
    Keep in mind you can't access the private object
    Are you sure about that?

  10. #8
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit keep cursor active.

    If you include qlineedit_p.h you can but you shouldn't because it's an internal file and it may be changed at any time.

    From qlineedit_p.h:

    Qt Code:
    1. // W A R N I N G
    2. // -------------
    3. //
    4. // This file is not part of the Qt API. It exists purely as an
    5. // implementation detail. This header file may change from version to
    6. // version without notice, or even be removed.
    7. //
    8. // We mean it.
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to victor.fernandez for this useful post:

    bunjee (2nd September 2009)

  12. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QLineEdit keep cursor active.

    Feels like using a tank to kill a fly.

    What would you do ?

  13. #10
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit keep cursor active.

    Yes it is. But if you don't do so, you have the risk that your application doesn't compile with future versions of Qt without changing the source code.

  14. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLineEdit keep cursor active.

    It's not that easy. If you include lineedit_p.h, you have to include qwidget_p.h and qobject_p.h. They in turn need a bunch of other includes.

    It's much easier to construct QTextLine and QTextLayout yourself and use them to draw the line of text with the cursor. It's really simple.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 10
    Last Post: 12th February 2009, 07:23
  2. problem with QLineEdit and QPalette
    By impeteperry in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 17:05
  3. QLineEdit
    By rick_st3 in forum Newbie
    Replies: 1
    Last Post: 14th June 2008, 09:05
  4. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  5. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.