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

来源:互联网 发布:巨杉数据库nosql 编辑:程序博客网 时间:2024/05/24 06:50
經常我們有這樣的需求:比如文本居中顯示,背景色,文本顔色的設置...等等這些樣式設定.
之前寫過一個繼承自QDirModel的類(見下文).我們需要改動的地方就是QVariant QDirModel::data(const QModelIndex index,int role = Qt::DisplayRole) const   [virtual]
這個函數.察看Qt Assistant可以看到,enumQt::ItemDataRole

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

ConstantValueDescriptionQt::DisplayRole0The key data to be rendered inthe form of text. (QString)Qt::DecorationRole1The data to be rendered as adecoration in the form of an icon. (QColor,QIcon or QPixmap)Qt::EditRole2The data in a form suitable forediting in an editor. (QString)Qt::ToolTipRole3The data displayed in the item'stooltip. (QString)Qt::StatusTipRole4The data displayed in the statusbar. (QString)Qt::WhatsThisRole5The data displayed for the itemin "What's This?" mode. (QString)Qt::SizeHintRole13The size hint for the item thatwill be supplied to views. (QSize)

Roles describing appearance andmeta data (with associated types):

ConstantValueDescriptionQt::FontRole6The font used for items renderedwith the default delegate. (QFont)Qt::TextAlignmentRole7The alignment of the text foritems rendered with the default delegate. (Qt::AlignmentFlag)Qt::BackgroundRole8The background brush used foritems rendered with the default delegate. (QBrush)Qt::BackgroundColorRole8This role is obsolete. UseBackgroundRole instead.Qt::ForegroundRole9The foreground brush (textcolor, typically) used for items rendered with the defaultdelegate. (QBrush)Qt::TextColorRole9This role is obsolete. UseForegroundRole instead.Qt::CheckStateRole10This role is used to obtain thechecked state of an item. (Qt::CheckState)

Accessibility roles (withassociated types):

ConstantValueDescriptionQt::AccessibleTextRole11The text to be used byaccessibility extensions and plugins, such as screen readers.(QString)Qt::AccessibleDescriptionRole12A description of the item foraccessibility purposes. (QString)

User roles:

ConstantValueDescriptionQt::UserRole32The first role that can be usedfor application-specific purposes.我們根據index和role的不同,返回不同的QVariant給程序,就可以對特定index下的特定role設置自己想要的值,這樣就可以隨心所欲的去實現我們的期望了: 
注意:針對不同的role,在
Description中都有説明是什麽類型,根據這個來決定return的QVariant值.
class MyDirModel : publicQDirModel 
{
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,introle)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,introle)const
{
if(role == Qt::DisplayRole &&orientation == Qt::Horizontal)
return header[section];
return QDirModel::headerData(section,orientation,role);
}

};
 
0 0
原创粉丝点击