Sections

How can I modify the color of some of the rows in my view ?

 

Answer:

You can achieve this by reimplementing the delegate's paint() function and then modifying the palette for the rows you want. See the following example:

#include <QtGui>

class ItemDelegate: public QItemDelegate
{
public:
	ItemDelegate()
	{
	}

	void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const  
	{
		QPalette pal = option.palette;
		QStyleOptionViewItem  viewOption(option);
		if(index.row() == 0) {
			viewOption.palette.setColor(QPalette::HighlightedText, Qt::red);
		} else if (index.row() == 5) {
			viewOption.palette.setColor(QPalette::HighlightedText, Qt::blue);
		} else {
			viewOption.palette.setColor(QPalette::HighlightedText, Qt::white);
		}
		QItemDelegate::paint(painter, viewOption, index);
	}


};

int main(int argc, char** argv)
{
	QApplication app(argc, argv);
	QTableWidget table(10,10);
	table.setItemDelegate(new ItemDelegate());
	QTableWidgetItem *item1= new QTableWidgetItem();
	item1->setText("item 1");
	table.setItem(0, 0, item1);
	table.show();
	return app.exec();
}

See the documentation:

http://doc.trolltech.com/4.3/qitemdelegate.html#paint

If you want to change the color of the items in your model, then see:

http://trolltech.com/developer/knowledgebase/faq.2007-04-25.9332633815/

back

Patron of KDECustomers

Customers