[PyQt] Custom QSqlRelationalDelagate: how to change text?

Simone Zaccarin simozack at yahoo.it
Fri Apr 11 14:29:06 BST 2008


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'. 

I think to overwrite the paint method of the QSqlRelationalDelegate, but the result is not what I'm expeting: the text and the background of the cells are black. 

How can I simple change the text without changing anything else in the paint method? Has anyone an example on that?

This is the source of my custom paint method (I derived this the code from the PyQt book examples):

def paint(self, painter, option, index):
        if index.column() == 1:
            myoption = QStyleOptionViewItem(option)
            text = index.model().data(index).toString()
            palette = QApplication.palette()
            document = QTextDocument()
            document.setDefaultFont(option.font)
            document.setDefaultTextOption(option.color)
            value_list = text.split('-')
            year, month, day = value_list
            date = QString(QDate(int(year), int(month), int(day)).toString('dd/MM/yyyy'))
            document.setPlainText(date)
            color = palette.highlight().color() if option.state & QStyle.State_Selected \
                    else QColor(index.model().data(index, Qt.BackgroundColorRole))
            painter.save()
            painter.fillRect(option.rect, color)
            painter.translate(option.rect.x(), option.rect.y())
            document.drawContents(painter)
            painter.restore()
        else:
            QSqlRelationalDelegate.paint(self, painter, option, index)

I'm on WinXp SP2, PyQt 4.3.3 and Python 2.5.2.
Thanks,
Simone




      Inviato da Yahoo! Mail. 
La casella di posta intelligente.
http://it.docs.yahoo.com/mail/overview/index.html



More information about the PyQt mailing list