View Full Version : Text Color on Model/View s

13th September 2011, 22:38
I am working through model/view programming and cannot figure out how to modify text color of "111, 222, 333" from the following example. Should I be looking at the QStandardItemModel documentation or QList? Thanks!

// modelview.cpp
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>
#include "mainwindow.h"

const int ROWS = 2;
const int COLUMNS = 3;

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
treeView = new QTreeView(this);
standardModel = new QStandardItemModel ;

QList<QStandardItem *> preparedRow =prepareRow("first", "second", "third");
QStandardItem *item = standardModel->invisibleRootItem();
// adding a row to the invisible root item produces a root element

QList<QStandardItem *> secondRow =prepareRow("111", "222", "333");
// adding a row to an item starts a subtree


QList<QStandardItem *> MainWindow::prepareRow(const QString &first,
const QString &second,
const QString &third)
QList<QStandardItem *> rowItems;
rowItems << new QStandardItem(first);
rowItems << new QStandardItem(second);
rowItems << new QStandardItem(third);
return rowItems;

13th September 2011, 22:53
void QStandardItem::setForeground ( const QBrush & brush )

Sets the brush used to display the item's foreground (e.g. text) to the given brush.

Did you try it?

13th September 2011, 23:54
I'm adding

item->setForeGround(Qt::red); //NEW

and no go? It builds but colors don't change.

14th September 2011, 00:22

rowItems << new QStandardItem(first);
rowItems << new QStandardItem(second);
rowItems << new QStandardItem(third);

Try to set foreground for a particular item containing text.

14th September 2011, 00:28
You need to set it on the items you are currently creating anonymously in prepareRow(). Items are individually coloured, it is not inherited from sibling or parent items.

You may also be able to change the entire table colouring using a style sheet.

14th September 2011, 04:04
Thanks guys I figured it out!

24th March 2015, 06:46
how man... thank you for not just said you find it, but also show your code resolved the problem.... the other one are really happy you share (good thinking... very great).
each time i read someone has same problem than me and share the solution like you... i'm very happy. In my mind, i tell me that this guys not just think about himself.

so, for me, by follow the idea, i do a:

QStandardItem *a = new QStandardItem("new one");
a->setForeground(QBrush(Qt::red, Qt::SolidPattern));

(that is the view of the solution i share with you) :)
so this works just fine.