구리의 창고

QT Programming - QTreeView 색 변경 본문

QT

QT Programming - QTreeView 색 변경

구리z 2011. 7. 26. 14:57
QTreeView 의 글씨색을 변경해보자.

QTreeWidget 의 경우, setTextColor 라는 함수가 존재한다.


하지만 QTreeView에는 그런거 없다.

QStyle 따위를 사용하면 될지 안해봤지만.. 그보다 좋은 방법이 있다.

setData 중  적절한 Role을 세팅해주는 것이다.

자세한 내용은 아래 링크를 참고하면된다.

http://doc.qt.nokia.com/4.7/qt.html#ItemDataRole-enum

  1. setData(QColor(255, 0, 0), Qt::TextColorRole)  
정도로 변경해주면된다.
 

enum Qt::ItemDataRole

Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

The general purpose roles (and the associated types) are:

ConstantValueDescription
Qt::DisplayRole 0 The key data to be rendered in the form of text. (QString)
Qt::DecorationRole 1 The data to be rendered as a decoration in the form of an icon. (QColorQIcon or QPixmap)
Qt::EditRole 2 The data in a form suitable for editing in an editor. (QString)
Qt::ToolTipRole 3 The data displayed in the item's tooltip. (QString)
Qt::StatusTipRole 4 The data displayed in the status bar. (QString)
Qt::WhatsThisRole 5 The data displayed for the item in "What's This?" mode. (QString)
Qt::SizeHintRole 13 The size hint for the item that will be supplied to views. (QSize)

Roles describing appearance and meta data (with associated types):

ConstantValueDescription
Qt::FontRole 6 The font used for items rendered with the default delegate. (QFont)
Qt::TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt::AlignmentFlag)
Qt::BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush)
Qt::BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead.
Qt::ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush)
Qt::TextColorRole 9 This role is obsolete. Use ForegroundRole instead.
Qt::CheckStateRole 10 This role is used to obtain the checked state of an item. (Qt::CheckState)

Accessibility roles (with associated types):

ConstantValueDescription
Qt::AccessibleTextRole 11 The text to be used by accessibility extensions and plugins, such as screen readers. (QString)
Qt::AccessibleDescriptionRole 12 A description of the item for accessibility purposes. (QString)

User roles:

ConstantValueDescription
Qt::UserRole 32 The first role that can be used for application-specific purposes.

Comments