Results 1 to 7 of 7

Thread: QLineEdit selectAll() in eventFilter

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QLineEdit selectAll() in eventFilter

    I'm having trouble selecting all the text in a QLineEdit control inside an eventFilter.

    In a QDialog, I want to select all text in a QLineEdit control when a user tabs into it. My eventFilter recognizes the control but doesn't select.

    Qt Code:
    1. bool AddMainPage::eventFilter( QObject* pObj, QEvent* pEvent )
    2. {
    3. if ( pEvent->type() == QEvent::FocusIn )
    4. {
    5. if ( pObj == ui.ipAddrEdt ) // ipAddrEdt has focus at this point
    6. {
    7. ui.ipAddrEdt->selectAll();
    8. ui.ipAddrEdt->setCursorPosition( 0 );
    9. return true;
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 
    I'm not sure that I'm going about this in the correct way either. I don't want to selectAll() when the user uses the mouse to click into the control, only when it's tabbed into. I know that I'm dealing with 2 different events here; QEvent::FocusIn and QEvent::KeyPress.

    Does anyone have any suggestions on how best to handle this situation?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit selectAll() in eventFilter

    Qt Code:
    1. QFocusEvent* pFocusEvent = static_cast<QFocusEvent*>(pEvent);
    2. if (pFocusEvent->reason() == Qt::TabFocusReason)
    3. {
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    mclark (29th November 2007)

  4. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit selectAll() in eventFilter

    Thanks jpn, that solves the design problem, now, does anyone know why the selectAll() does not select anything in my QLineEdit object?

    It should be noted that I have an input mask on this object like: 000.000.000.000, a standard 4 segment IP address. What is displayed in the control is "0 .0 .0 .0 ". If I don't use the eventFilter, the first character is selected when the control is tabbed into. If using the eventFilter, no selection occurs .

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QLineEdit selectAll() in eventFilter

    I'm afraid QLineEdit::setCursorPos(0) clears the selection.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    mclark (29th November 2007)

  7. #5
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit selectAll() in eventFilter

    Good catch jpn, I was hoping it would be a foolish mistake on my part that someone would notice. I works just like I want now. Thanks!

  8. #6
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post

    Default Re: QLineEdit selectAll() in eventFilter

    Quote Originally Posted by mclark View Post
    Thanks jpn, that solves the design problem, now, does anyone know why the selectAll() does not select anything in my QLineEdit object?
    ....
    If using the eventFilter, no selection occurs .
    Did you ever figure out how to make this work?

    I just set up an eventFilter, but selectAll() doesn't work... clear() does, as does changing the color... so I know I'm catching the event and object... but selectAll() doesn't do anything.

    Qt Code:
    1. bool Track::eventFilter(QObject *obj, QEvent *e) {
    2. // qDebug() << "Track::eventFilter: Received event '" << e->type() << "' from object '" << obj->name() << "'.";
    3.  
    4. if (obj == m_pView->m_pLineEditSearch && e->type()==QEvent::FocusIn) {
    5. qDebug("QEvent::FocusIn event intercepted");
    6. ((QLineEdit *)obj)->selectAll();
    7. // ((QLineEdit *)obj)->clear();
    8. return false;
    9. } else if (obj == m_pView->m_pLineEditSearch && e->type()==QEvent::FocusOut) {
    10. qDebug("QEvent::FocusOut event intercepted");
    11. return false;
    12. }
    13. // else {
    14. // standard event processing
    15. // return QObject::eventFilter(obj, event);
    16. // }
    17. return false;
    18. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post

    Default Re: QLineEdit selectAll() in eventFilter

    seems the solution is to eat MouseButtonPress and MouseButtonRelease events...

    Qt Code:
    1. bool Track::eventFilter(QObject *obj, QEvent *e) {
    2. if (obj == m_pView->m_pLineEditSearch) {
    3. // qDebug() << "Track::eventFilter: Received event '" << e->type() << "' from object '" << obj->name() << "'.";
    4. if (e->type()==QEvent::FocusIn) {
    5. // qDebug("QEvent::FocusIn event intercepted");
    6. ((QLineEdit *)obj)->selectAll();
    7. // qDebug() << "hasSelectedText? " << ((QLineEdit *)obj)->hasSelectedText();
    8. // qDebug() << "So the selected text is now: " << ((QLineEdit *)obj)->selectedText();
    9. } else if (e->type()==QEvent::MouseButtonPress || e->type()==QEvent::MouseButtonRelease) { // Drop entry events which deselect the text
    10. // qDebug("Drop mouse event.");
    11. return true;
    12. }
    13. }
    14. return false;
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. 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.