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.
Code:
{
if ( pEvent
->type
() == QEvent::FocusIn ) {
if ( pObj == ui.ipAddrEdt ) // ipAddrEdt has focus at this point
{
ui.ipAddrEdt->selectAll();
ui.ipAddrEdt->setCursorPosition( 0 );
return true;
}
...
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?
Re: QLineEdit selectAll() in eventFilter
Code:
QFocusEvent* pFocusEvent
= static_cast<QFocusEvent
*>
(pEvent
);
if (pFocusEvent->reason() == Qt::TabFocusReason)
{
...
}
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 :confused:.
Re: QLineEdit selectAll() in eventFilter
I'm afraid QLineEdit::setCursorPos(0) clears the selection.
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!
Re: QLineEdit selectAll() in eventFilter
Quote:
Originally Posted by
mclark
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 :confused:.
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. :(
Code:
// qDebug() << "Track::eventFilter: Received event '" << e->type() << "' from object '" << obj->name() << "'.";
if (obj
== m_pView
->m_pLineEditSearch
&& e
->type
()==QEvent::FocusIn) { qDebug("QEvent::FocusIn event intercepted");
// ((QLineEdit *)obj)->clear();
return false;
} else if (obj
== m_pView
->m_pLineEditSearch
&& e
->type
()==QEvent::FocusOut) { qDebug("QEvent::FocusOut event intercepted");
return false;
}
// else {
// standard event processing
// return QObject::eventFilter(obj, event);
// }
return false;
}
Re: QLineEdit selectAll() in eventFilter
seems the solution is to eat MouseButtonPress and MouseButtonRelease events...
Code:
if (obj == m_pView->m_pLineEditSearch) {
// qDebug() << "Track::eventFilter: Received event '" << e->type() << "' from object '" << obj->name() << "'.";
if (e
->type
()==QEvent::FocusIn) { // qDebug("QEvent::FocusIn event intercepted");
// qDebug() << "hasSelectedText? " << ((QLineEdit *)obj)->hasSelectedText();
// qDebug() << "So the selected text is now: " << ((QLineEdit *)obj)->selectedText();
} else if (e
->type
()==QEvent::MouseButtonPress || e
->type
()==QEvent::MouseButtonRelease) { // Drop entry events which deselect the text // qDebug("Drop mouse event.");
return true;
}
}
return false;
}