#include #include #include #include #include class Delegate : public QItemDelegate { // Q_OBJECT public: Delegate(QObject *parent) : QItemDelegate(parent) {} virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { const QWidget *widget = qstyleoption_cast(&option)->widget; QStyle *style = widget->style(); style->drawControl(QStyle::CE_ItemViewItem, &option, painter, widget); // It gives wrong color when using Aero. QPalette::ColorRole colorRole = (option.state & QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::Text; painter->setPen(option.palette.color(QPalette::Normal, colorRole)); painter->drawText(option.rect, index.data(Qt::DisplayRole).toString()); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QTreeView view; QFileSystemModel *model = new QFileSystemModel(&view); model->setRootPath(QDir::homePath()); view.setModel(model); // Compare without this: view.setItemDelegate(new Delegate(&view)); view.show(); return a.exec(); }