<div class="gmail_quote">Hello, I've been struggling getting <a href="http://doc.trolltech.com/4.6/qt.html" target="_blank">ForegroundRole</a> to work properly so I can change the text color of certain cells in QTableView.<div>
I took an example from <a href="http://www.daniweb.com/forums/thread191210-7.html" target="_blank">http://www.daniweb.com/forums/thread191210-7.html</a> and added the following in the data(...) method:</div>
<div><br></div><div><div> elif role == Qt.ForegroundRole:</div><div> return QBrush(QColor(255, 0, 0))</div><div><br></div><div>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.</div>
<div><br></div><div>Is there any Maemo-specific calls that I am missing here?</div><div><br></div><div><br></div></div><div><div><font face="'courier new', monospace"><br></font></div><div>
<div><font face="'courier new', monospace"># <a href="http://www.daniweb.com/forums/thread191210-7.html" target="_blank">http://www.daniweb.com/forums/thread191210-7.html</a></font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">import operator</font></div><div><font face="'courier new', monospace">from PyQt4.QtCore import *</font></div>
<div><font face="'courier new', monospace">from PyQt4.QtGui import *</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">class MyWindow(QWidget):</font></div>
<div><font face="'courier new', monospace"> def __init__(self, data_list, header, *args):</font></div><div><font face="'courier new', monospace"> QWidget.__init__(self, *args)</font></div>
<div><font face="'courier new', monospace"> # setGeometry(x_pos, y_pos, width, height)</font></div><div><font face="'courier new', monospace"> self.setGeometry(300, 200, 420, 250)</font></div>
<div><font face="'courier new', monospace"> self.setWindowTitle("Exploring PyQT's QTableView")</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace"> table_model = MyTableModel(self, data_list, header)</font></div><div><font face="'courier new', monospace"> table_view = QTableView()</font></div>
<div><font face="'courier new', monospace"> table_view.setModel(table_model)</font></div><div><font face="'courier new', monospace"> # enable sorting</font></div>
<div><font face="'courier new', monospace"> table_view.setSortingEnabled(True)</font></div><div><font face="'courier new', monospace"><br></font></div>
<div><font face="'courier new', monospace"> layout = QVBoxLayout(self)</font></div><div><font face="'courier new', monospace"> layout.addWidget(table_view)</font></div>
<div><font face="'courier new', monospace"> self.setLayout(layout)</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">class MyTableModel(QAbstractTableModel):</font></div><div><font face="'courier new', monospace"> def __init__(self, parent, mylist, header, *args):</font></div>
<div><font face="'courier new', monospace"> QAbstractTableModel.__init__(self, parent, *args)</font></div><div><font face="'courier new', monospace"> self.mylist = mylist</font></div>
<div><font face="'courier new', monospace"> self.header = header</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> def rowCount(self, parent):</font></div>
<div><font face="'courier new', monospace"> return len(self.mylist)</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> def columnCount(self, parent):</font></div>
<div><font face="'courier new', monospace"> return len(self.mylist[0])</font></div><div><font face="'courier new', monospace"><br></font></div><div>
<font face="'courier new', monospace"> def data(self, index, role):</font></div><div><font face="'courier new', monospace"> if not index.isValid():</font></div>
<div><font face="'courier new', monospace"> return QVariant()</font></div><div><font face="'courier new', monospace"> # ---------------------------------------------</font></div>
<div><font face="'courier new', monospace"> # Works on Windows PC but not on N900</font></div><div><font face="'courier new', monospace"> # ---------------------------------------------</font></div>
<div><font face="'courier new', monospace"> elif role == Qt.ForegroundRole:</font></div><div><font face="'courier new', monospace"> return QBrush(QColor(255, 0, 0))</font></div>
<div><font face="'courier new', monospace"> elif role != Qt.DisplayRole:</font></div><div><font face="'courier new', monospace"> return QVariant()</font></div>
<div><font face="'courier new', monospace"> return QVariant(self.mylist[index.row()][index.column()])</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace"> def headerData(self, col, orientation, role):</font></div><div><font face="'courier new', monospace"> if orientation == Qt.Horizontal and role == Qt.DisplayRole:</font></div>
<div><font face="'courier new', monospace"> return QVariant(self.header[col])</font></div><div><font face="'courier new', monospace"> return QVariant()</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> def sort(self, col, order):</font></div><div><font face="'courier new', monospace"> """sort table by given column number col"""</font></div>
<div><font face="'courier new', monospace"> self.emit(SIGNAL("layoutAboutToBeChanged()"))</font></div><div><font face="'courier new', monospace"> self.mylist = sorted(self.mylist,</font></div>
<div><font face="'courier new', monospace"> key=operator.itemgetter(col))</font></div><div><font face="'courier new', monospace"> if order == Qt.DescendingOrder:</font></div>
<div><font face="'courier new', monospace"> self.mylist.reverse()</font></div><div><font face="'courier new', monospace"> self.emit(SIGNAL("layoutChanged()"))</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">header = ['First Name', 'Last Name', 'Age', 'Weight']</font></div>
<div><font face="'courier new', monospace"># a list of (name, age, weight) tuples</font></div><div><font face="'courier new', monospace">data_list = [</font></div>
<div><font face="'courier new', monospace">('Heidi', 'Kalumpa', '36', '127'),</font></div><div><font face="'courier new', monospace">('Frank', 'Maruco', '27', '234'),</font></div>
<div><font face="'courier new', monospace">('Larry', 'Pestraus', '19', '315'),</font></div><div><font face="'courier new', monospace">('Serge', 'Romanowski', '59', '147'),</font></div>
<div><font face="'courier new', monospace">('Carolus', 'Arm', '94', '102'),</font></div><div><font face="'courier new', monospace">('Michel', 'Sargnagel', '21', '175')</font></div>
<div><font face="'courier new', monospace">]</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">app = QApplication([])</font></div>
<div><font face="'courier new', monospace">win = MyWindow(data_list, header)</font></div><div><font face="'courier new', monospace">win.show()</font></div>
<div><font face="'courier new', monospace">app.exec_()</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"><br>
</font></div><div><br></div></div></div>
<br></div><br>