PDA

View Full Version : how to change cursor position in an QDateEdit after mouse click on it?



javimoya
9th July 2009, 22:13
Hi !
(sorry for my poor english)

I'm new to QT and c++ (just 2 days!)... but I love it !


this is my question:

I have a QDateEdit in my dialog...
with this format: dd/MM/yyyy
I want that when my QDateEdit get the focus (tab, mouse click, etc), select the third section (yyyy)...

QDateEdit has not a slot for focus issues... so... I tried this:


...
dateEdit->installEventFilter(this);
....


bool Ventana::eventFilter(QObject *obj, QEvent *event)

{

if (event->type() == QEvent::FocusIn)
{
QFocusEvent *event2 ;
event2 = dynamic_cast< QFocusEvent * >( event );
if (event2->reason() != Qt::ActiveWindowFocusReason)
{

dateEdit->setCurrentSectionIndex(2);
dateEdit->setSelectedSection(dateEdit->currentSection());
return true;
}
else
{
return QObject::eventFilter(obj, event);
}
}

else
{
return QObject::eventFilter(obj, event);
}

}

and this works perfectly always... but when the focus is given by a mouse click...
In that case no section is selected... just the cursor is positioned in the place the mouse clicks (as usual)...

I think that after a mouse click another event or slot or something else is executing after the focusIn event... losing my selected section...
But I don't know how to solve this....
any idea?

Thank you very much

shentian
10th July 2009, 09:54
I think that after a mouse click another event or slot or something else is executing after the focusIn event... losing my selected section...
But I don't know how to solve this....
any idea?

This is probably the MouseButtonPress event that arrives after the FocusIn event. You could try to suppress it in your event filter, but then you couldn't position the text cursor any more by clicking.