PDA

View Full Version : QLineEdit selectAll() in eventFilter



mclark
29th November 2007, 20:21
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.


bool AddMainPage::eventFilter( QObject* pObj, QEvent* pEvent )
{
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?

jpn
29th November 2007, 20:49
QFocusEvent* pFocusEvent = static_cast<QFocusEvent*>(pEvent);
if (pFocusEvent->reason() == Qt::TabFocusReason)
{
...
}

mclark
29th November 2007, 20:55
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:.

jpn
29th November 2007, 21:07
I'm afraid QLineEdit::setCursorPos(0) clears the selection.

mclark
29th November 2007, 21:11
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!

ironstorm
1st February 2008, 07:06
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. :(


bool Track::eventFilter(QObject *obj, QEvent *e) {
// 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)->selectAll();
// ((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;
}

ironstorm
1st February 2008, 08:13
seems the solution is to eat MouseButtonPress and MouseButtonRelease events...


bool Track::eventFilter(QObject *obj, QEvent *e) {
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");
((QLineEdit *)obj)->selectAll();
// 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;
}