[PyQt] How to clear QDateTime

David Boddie david at boddie.org.uk
Sun Feb 1 21:24:08 GMT 2009


On Sun Feb 1 20:53:45 GMT 2009, David Douard wrote:
> Le Saturday 31 January 2009 19:15:23 Marc Nations, vous avez écrit :

> > I can't seem to clear out the QDateTime field in cases where I don't want
> > the date displayed. Using clear() seems to wipe out the first entry (in
> > this case the hour). I tried hitting multiple clears thinking that it may
> > just work down the list, but that didn't work. Is there a way to blank
> > out the field completely? I know it can be disbled, but I'd like to have
> > it be blank until the user goes and selects the pop-up calendar.
>
> This is a gap in Qt, and it won't happen before a while. See
> http://www.qtsoftware.com/developer/task-tracker/index_html?method=entry&id
>=135683

One way to do this is to access the QLineEdit child widget of the 
QDateTimeEdit and clear that instead, like this:

  dateTimeEdit = QDateTimeEdit()
  lineEdit = dateTimeEdit.findChild(QLineEdit)
  lineEdit.setText("")

Note that this puts the QDateTimeEdit into a state where validation is not
used, so you need to make sure that it's enabled again before you let the
user enter text. Perhaps disable the QDateTimeEdit as well to prevent that
from happening and set the current section when you enable it again.

As with all these hacks to get at the widgets behind the scenes, it's not
guaranteed to work, either now or in the future, but it may prove to be a
useful workaround if it happens to work for you.

David



More information about the PyQt mailing list