前言
原因:记录QT学习中遇到的问题,方便记录回看。
环境:vs2017+QT5.9.1
一、QListView使用
示例:在项目中用QListView做历史记录的显示,用MyListDelegate做QListView的样式,用QAbstractItemModel做显示的内容。
二、代码示例
代码如下(示意函数用法,不能单独运行):
1.显示的内容类
class MyModel : public QAbstractItemModel
{
...
private:
QList<MyInfo *> m_items;//MyInfo是具体的内容类
...
}
2.显示的代理类
MyListDelegate.h
class MyListDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MyListDelegate(QObject *parent = nullptr);
~MyListDelegate();
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void paint(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QString loadFontFamilyFromFiles(const QString &fontFileName) const;
public:
QPixmap pixmap;
QString family;
QPixmap normal;
QPixmap red;
QPixmap yellow;
}<