[PyQt] Custom QSqlRelationalDelagate: how to change text?

Simone Zaccarin simozack at yahoo.it
Fri Apr 11 15:34:57 BST 2008

My only problem is that I construct the UI via QtDesigner. Is there a way to "overwrite" the table added via Designer without being a PyQt4 expert?



----- Messaggio originale -----
Da: Mark Summerfield <mark at qtrac.eu>
A: pyqt at riverbankcomputing.com
Cc: Simone Zaccarin <simozack at yahoo.it>
Inviato: Venerdì 11 aprile 2008, 16:02:12
Oggetto: Re: [PyQt] Custom QSqlRelationalDelagate: how to change text?

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
    dateString = index.model().data(index).toString() return
    return QDate.fromString(dateString, "yyyy-MM-dd").toString("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
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Mark Summerfield, Qtrac Ltd., www.qtrac.eu

      Inviato da Yahoo! Mail. 
La casella di posta intelligente.

More information about the PyQt mailing list