[maemo-developers] PyQt ForegroundRole

From: SC shing.n900 at gmail.com
Date: Thu Apr 29 13:16:05 EEST 2010
Hello, I've been struggling getting
ForegroundRole<http://doc.trolltech.com/4.6/qt.html>to work properly
so I can change the text color of certain cells in
I took an example from http://www.daniweb.com/forums/thread191210-7.html and
added the following in the data(...) method:

        elif role == Qt.ForegroundRole:
            return QBrush(QColor(255, 0, 0))

When I run it on my Windows PC, the font color is set to red for all the
cells but does not seem to have an effect on my N900.

Is there any Maemo-specific calls that I am missing here?

# http://www.daniweb.com/forums/thread191210-7.html

import operator
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWindow(QWidget):
    def __init__(self, data_list, header, *args):
        QWidget.__init__(self, *args)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(300, 200, 420, 250)
        self.setWindowTitle("Exploring PyQT's QTableView")

        table_model = MyTableModel(self, data_list, header)
        table_view = QTableView()
        # enable sorting

        layout = QVBoxLayout(self)

class MyTableModel(QAbstractTableModel):
    def __init__(self, parent, mylist, header, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.mylist = mylist
        self.header = header

    def rowCount(self, parent):
        return len(self.mylist)

    def columnCount(self, parent):
        return len(self.mylist[0])

    def data(self, index, role):
        if not index.isValid():
            return QVariant()
        # ---------------------------------------------
        # Works on Windows PC but not on N900
        # ---------------------------------------------
        elif role == Qt.ForegroundRole:
            return QBrush(QColor(255, 0, 0))
        elif role != Qt.DisplayRole:
            return QVariant()
        return QVariant(self.mylist[index.row()][index.column()])

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return QVariant(self.header[col])
        return QVariant()

    def sort(self, col, order):
        """sort table by given column number col"""
        self.mylist = sorted(self.mylist,
        if order == Qt.DescendingOrder:

header = ['First Name', 'Last Name', 'Age', 'Weight']
# a list of (name, age, weight) tuples
data_list = [
('Heidi', 'Kalumpa', '36', '127'),
('Frank', 'Maruco', '27', '234'),
('Larry', 'Pestraus', '19', '315'),
('Serge', 'Romanowski', '59', '147'),
('Carolus', 'Arm', '94', '102'),
('Michel', 'Sargnagel', '21', '175')

app = QApplication([])
win = MyWindow(data_list, header)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.maemo.org/pipermail/maemo-developers/attachments/20100429/95d36de2/attachment.htm>
More information about the maemo-developers mailing list