PyQT / QDate / QTableWidget

David Boddie dboddie at trolltech.com
Fri Mar 28 09:12:04 EDT 2008


On Wed Mar 26 15:13:09 CET 2008, wrightee wrote:

> My server gives me a string y[0]: "20080327", which I convert to a
> QDateTime object using:
> 
> x=QDateTime.fromString(y[0],"yyyymmdd")
> 
> Printing x.toString("dd-mm-yyyy") gives me what I would expect -
> 27-03-2008

Strange. You should really be using "dd-MM-yyyy". Maybe this is why QDate
isn't behaving as you would expect - see below.

> What I'm trying to do though is add this to a QTableWidget item to
> create a date sortable column; I'm using this:
> 
> if type(y)==QDateTime:
>                         item=QTableWidgetItem()
>                         item.setData(Qt.DisplayRole,QVariant(y))

This should work fine.

> BUT.. I'm adding 90 dates going back from today and getting values
> that look like this:
> 
> 27/01/2007 00:12
> 28/01/2007 00:12
> 29/01/2007 00:12
> 30/01/2007 00:12
> 31/01/2007 00:12
> 01/01/2008 00:01
> 01/01/2008 00:02
> 01/01/2008 00:03
> 
> etc

Right. You can see the date and time because you're using a QDateTime object.

> I tried using QDate but couldn't seem to be able to get
> QDate.fromString to create an object at all.

This may have something to do with the format string you tried.

> Could someone please advise where I'm going wrong, the end result
> should be a column in my QTableWidget formatted dd/mm/yyyy that can be
> sorted as dates, not strings, and originate from data formatted
> "YYYYMMDD"

Just use QDate objects instead of QDateTime objects and it should all just
work.

Good luck!

David
-- 
David Boddie
Lead Technical Writer, Trolltech ASA



More information about the Python-list mailing list