[PyQt] Custom QSqlRelationalDelagate: how to change text?

Simone simozack at yahoo.it
Sat Apr 12 08:45:54 BST 2008


Mark Summerfield ha scritto:
> On 2008-04-11, Simone Zaccarin wrote:
>> I'm trying to create a custom QSqlRelationalDelegate.
>>
>> In the database, the date field is in text form 'yyyy-MM-dd'. I have to
>> show that string in a QSqlTable in the form 'dd-MM-yyyy'.
> 
> If that's the only change you want to make, you could try what might be
> a simpler approach, such as subclassing QRelationalTableModel and
> reimplementing the data() method. For every column except the date just
> pass the work to the base class, and for your date column do something
> like:
>     
>     dateString = index.model().data(index).toString() return
>     return QDate.fromString(dateString, "yyyy-MM-dd").toString("dd/MM/yyyy")

Thanks, Mark! :D

This is the final version:

def data(self, index, role):
         if index.column() == 1 and role == Qt.DisplayRole:
             try:
                 dateString = QSqlRelationalTableModel.\
			     data(self, index, role).toString()
                 date = QDate.fromString(dateString,
			(yyyy-MM-dd").toString("dd/MM/yyyy")
                 return QVariant(date)
             except ValueError:
                 return QSqlRelationalTableModel.data(self, index, role)
         else:
             return QSqlRelationalTableModel.data(self, index, role)

I have to get the string value with the QSqlRelationalTableModel.data 
method because otherwise I obtain a recursion without end.

Thanks a lot,
Simone
Chiacchiera con i tuoi amici in tempo reale! 
 http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com 



More information about the PyQt mailing list