Qt Model & View的角色設定.(文本…

經常我們有這樣的需求:比如文本居中顯示,背景色,文本顔色的設置...等等這些樣式設定.
之前寫過一個繼承自QDirModel的類(見下文).我們需要改動的地方就是QVariant QDirModel::data ( const QModelIndex index, int role = Qt::DisplayRole ) const   [virtual]
這個函數.察看Qt Assistant可以看到,enum Qt::ItemDataRole

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

Constant Value Description
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. (QColor,QIcon 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):

Constant Value Description
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):

Constant Value Description
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:

Constant Value Description
Qt::UserRole 32 The first role that can be used for application-specific purposes.
我們根據index和role的不同,返回不同的QVariant給程序,就可以對特定index下的特定role設置自己想要的值,這樣就可以隨心所欲的去實現我們的期望了: 
注意:針對不同的role,在
Description 中都有説明是什麽類型,根據這個來決定return的QVariant值.
class MyDirModel : public QDirModel 
{
Q_OBJECT
public:
QStringList header;
MyDirModel( QObject * parent = 0 ):QDirModel(parent)
{
header<<tr("name")<<tr("size")<<tr("type")<<tr("date");    //這個主要是涉及到國際化.
}
~MyDirModel(){}

virtual int rowCount(const QModelIndex& parent = QModelIndex())const
 
{
return QDirModel::rowCount(parent);
}
virtual int columnCount(const QModelIndex& parent = QModelIndex())const
{
return QDirModel::columnCount(parent);
}
QVariant data(const QModelIndex& index,int role)const //可以對index和role一起進行設定
{
if (role == Qt::TextAlignmentRole )
{
return int(Qt::AlignHCenter | Qt::AlignVCenter);   //必須把Qt::AlignmentFlag轉換為int
}
else if(role == Qt::BackgroundRole)
{
QRadialGradient gradient(50, 50, 200);
gradient.setColorAt(0, QColor::fromRgb(92, 92, 255));
gradient.setColorAt(1, QColor::fromRgb(255, 92, 92));
QBrush brush(gradient);
return brush;
}
 

else
{
return QDirModel::data(index,role);
}
}
QVariant headerData(int section,Qt::Orientation orientation,int role)const
{
if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
return header[section];
return QDirModel::headerData(section,orientation,role);
}

};
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值