Qt Model & View的角色設定.(文本…
来源:互联网 发布:巨杉数据库nosql 编辑:程序博客网 时间:2024/05/24 06:50
經常我們有這樣的需求:比如文本居中顯示,背景色,文本顔色的設置...等等這些樣式設定.
之前寫過一個繼承自QDirModel的類(見下文).我們需要改動的地方就是QVariantQDirModel::data(const QModelIndex & index,int role = Qt::DisplayRole) const [virtual]
這個函數.察看Qt Assistant可以看到,enumQt::ItemDataRole
Constant Value Description Qt::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)Constant Value Description Qt::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)Constant Value Description Qt::AccessibleTextRole11The text to be used byaccessibility extensions and plugins, such as screen readers.(QString)Qt::AccessibleDescriptionRole 12A description of the item foraccessibility purposes. (QString)Constant Value Description Qt::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);
}
};
之前寫過一個繼承自QDirModel的類(見下文).我們需要改動的地方就是QVariant
The general purpose roles (and the associated types)are:
Roles describing appearance andmeta data (with associated types):
Accessibility roles (withassociated types):
User roles:
注意:針對不同的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);
}
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
- Qt Model & View的角色設定.(文本…
- Qt Model & View的角色設定.(文本…
- Tsai Camera Model&nbsp…
- ExecutionLog2 View -&n…
- [转] Qt属性setProperty & propert…
- [转] Qt属性setProperty & propert…
- 命令运行的判断依据: ; , &…
- 使用 grep 的 -o&n…
- weibo 登录&nbsp…
- Android edittext&nbsp…
- !!! FAILED BINDER&nbsp…
- OGNL & ValueStack&…
- View Controller 生命周期的…
- View Controller 生命周期的…
- LA 5713 Qin&nbsp…
- Convex Hull Algorithms & general…
- Qt:亲手写框体 C++ GUI (没课的…
- Javascript_DOMContentLoaded&nbsp…
- [转]Qt在Windows下添加语音输出功…
- [转] qt播放flash
- X11/Linux下发布Qt程序(Deploying …
- linux qt环境变量设置
- [转] Vim常用命令
- Qt Model & View的角色設定.(文本…
- Qt rand隨機色
- 10 Things You Need To Know About Cocoa Auto Layout
- [转] qt特效按鈕QPushButton
- [转] Qt configure參數
- qt StyleSheet设置滚动条样式表
- QSettings添加注册表项(Windows开…
- 前景图片alpha通道值的设置
- 安卓之Intent