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
QTableView.
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()
        table_view.setModel(table_model)
        # enable sorting
        table_view.setSortingEnabled(True)

        layout = QVBoxLayout(self)
        layout.addWidget(table_view)
        self.setLayout(layout)


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.emit(SIGNAL("layoutAboutToBeChanged()"))
        self.mylist = sorted(self.mylist,
            key=operator.itemgetter(col))
        if order == Qt.DescendingOrder:
            self.mylist.reverse()
        self.emit(SIGNAL("layoutChanged()"))


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)
win.show()
app.exec_()
_______________________________________________
maemo-developers mailing list
maemo-developers@maemo.org
https://lists.maemo.org/mailman/listinfo/maemo-developers

Reply via email to