对于一些数据量小的列表我们使用QListWidget往往能满足开发的需求,但是对于大数据量的展示来说(几十万,上百万)来说的话,全部加载是一个不是很合适的方法。因此我们使用MVC,这里可能应该叫MVD(model,view,delegation)。这样做个人认为有两个好处:

1.效率问题,view和delegation配合只负责从model中取显示需要的数据,因此带来高效。

2.model的数据可以由不同view来显示。

这里的话封装下通用QListView的通用方法(Tree和Table之后做),去除其中一些不必要业务逻辑(有空我自己精简下,目前没时间),可作为通用模板使用,基本就是三个类,有不明白的可以私信我。

一.View :.h文件

/// @brief 视图
class PreviewLeftWin:public QListView
{Q_OBJECT
public:PreviewLeftWin(QWidget* parent = 0);~PreviewLeftWin();void   Initialize();                               //总体初始化void   InitUi();                                   //初始化Uivoid   SwitchStyleSheet();                         //设置样式表void   ClearViewData();                            //清空当前视图的数据void   LoadData(QVector<QSharedPointer<IPhoto>>, QSharedPointer<IPhoto>);  //加载数据,丙切换选择到某数据private:virtual void startDrag(Qt::DropActions supportedActions) Q_DECL_OVERRIDE;     //开始拖拽事件virtual void mousePressEvent(QMouseEvent* e);                                 //鼠标按下事件signals:void SigShowResource(QSharedPointer<IPhoto>);   //选中项改变通知外部改变private:PreviewLeftWinModel*         m_model;     //模型PreviewLeftWinItemDelegate*  m_delegate;  //委托BFScrollBar*                 m_bfscroll;  //滚动条private slots:virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
};

View:cpp文件

PreviewLeftWin::PreviewLeftWin(QWidget* parent /*= 0*/) :QListView(parent)
{this->Initialize();
}PreviewLeftWin::~PreviewLeftWin()
{}void PreviewLeftWin::Initialize()
{//初始化UIthis->InitUi();//初始化Modelm_model = new PreviewLeftWinModel(this);this->setModel(m_model);//初始化委托m_delegate = new PreviewLeftWinItemDelegate(this);this->setItemDelegate(m_delegate);
}void PreviewLeftWin::InitUi()
{setAttribute(Qt::WA_TranslucentBackground, false);setAcceptDrops(false);setDragDropMode(QAbstractItemView::NoDragDrop);setDragEnabled(false);setDropIndicatorShown(false);setMouseTracking(true);setAutoFillBackground(true);setRowHidden(0, true);setViewMode(QListView::ListMode);setResizeMode(QListView::Adjust);setMovement(QListView::Snap);setSelectionMode(QAbstractItemView::SingleSelection);//样式表this->SwitchStyleSheet();//搭载自定义滚动条m_bfscroll = new BFScrollBar(Qt::Vertical, this);m_bfscroll->SetScrollArea(this);
}void PreviewLeftWin::SwitchStyleSheet()
{QString _bkStyle = QString("QListView{border:none;background-color:rgba(0,0,0,0);}QListView::item:selected{background-color:rgba(0,0,0,0);border:0px solid;}");setStyleSheet(_bkStyle);
}void PreviewLeftWin::ClearViewData()
{//model赋值数据m_model->ClearData();//刷新结构doItemsLayout();//刷新this->update();
}void PreviewLeftWin::LoadData(QVector<QSharedPointer<IPhoto>>iphoto_vec, QSharedPointer<IPhoto>ipoto)
{//读取新数据m_model->ClearData();m_model->SetUserData(iphoto_vec);this->clearSelection();//切换到选择的那个int index = iphoto_vec.indexOf(ipoto);QModelIndex qindex = m_model->index(index, 0);this->setCurrentIndex(qindex);//滚动条也要跟上
}void PreviewLeftWin::startDrag(Qt::DropActions supportedActions)
{return;
}void PreviewLeftWin::mousePressEvent(QMouseEvent* e)
{this->update();return QListView::mousePressEvent(e);
}void PreviewLeftWin::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
{//获取当前选中的QModelIndexList index_list = selected.indexes();if (index_list.isEmpty()){//第一次可能为空return;}if (index_list.size() != 1){Q_ASSERT(0);return;}QModelIndex cur_index = index_list[0];QSharedPointer<IPhoto> iphoto = cur_index.data(PreviewLeftWinModel_Data).value< QSharedPointer<IPhoto> >();if (!iphoto){Q_ASSERT(0);}SigShowResource(iphoto);
}

二.model:.h文件

class PreviewLeftWinModel : public QAbstractListModel
{Q_OBJECT
public:PreviewLeftWinModel(QObject* parent);~PreviewLeftWinModel();public:void ClearData(); //清空数据结构void SetUserData(QVector<QSharedPointer<IPhoto>>);//设置用户数据private:virtual QVariant data(const QModelIndex &index, int role) const;virtual Qt::ItemFlags  flags(const QModelIndex& index) const;virtual bool setData(const QModelIndex &index, const QVariant &value, int role);virtual QVariant headerData(int section, Qt::Orientation orientation,int role = Qt::DisplayRole) const;virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;virtual int columnCount(const QModelIndex& parent) const;
private:QVector<QSharedPointer<IPhoto>> m_data_vec;
};

model:.cpp文件

PreviewLeftWinModel::PreviewLeftWinModel(QObject* parent)
{ClearData();
}PreviewLeftWinModel::~PreviewLeftWinModel()
{}void PreviewLeftWinModel::ClearData()
{m_data_vec.clear();
}void PreviewLeftWinModel::SetUserData(QVector<QSharedPointer<IPhoto>>datas)
{m_data_vec = datas;
}//QVariant PreviewLeftWinModel::data(const QModelIndex &index,int role) const
{//无数据if (m_data_vec.size()==NULL){return QVariant();}//无效节点if( !index.isValid()){return QVariant();}//数据结构对应index(容器index)int row_index = index.row();if (row_index > m_data_vec.size() - 1){return QVariant();}//↓↓↓↓↓↓↓↓↓取数据↓↓↓↓↓↓↓↓↓↓if (role == PreviewLeftWinModel_Data){//取素材数据QSharedPointer<IPhoto> photo = m_data_vec.at(row_index);if ( photo != NULL ) {            return QVariant::fromValue(photo);}else{return QVariant(0);}}return QVariant();
}Qt::ItemFlags PreviewLeftWinModel::flags(const QModelIndex& index) const
{return /*QAbstractListModel::flags(index) | */Qt::ItemIsEnabled | Qt::ItemIsSelectable |Qt::ItemIsEditable /*| Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled*/;
}bool PreviewLeftWinModel::setData(const QModelIndex &index,const QVariant &value, int role)
{return true;
}QVariant PreviewLeftWinModel::headerData(int section, Qt::Orientation orientation,int role /*= Qt::DisplayRole*/) const
{return QVariant();
}int PreviewLeftWinModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{return m_data_vec.size();
}int PreviewLeftWinModel::columnCount(const QModelIndex& parent) const
{return 1;
}

三.Delegate:.h文件

/// @brief 委托
class PreviewLeftWinItemDelegate : public QStyledItemDelegate {Q_OBJECTpublic:PreviewLeftWinItemDelegate(QObject* parent = 0);virtual ~PreviewLeftWinItemDelegate();void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index);QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;void setEditorData(QWidget* editor, const QModelIndex& index) const;QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;private://类似网格布局需要根据rect_image来进一步适配QRect GetimageRect(QRect, QSize) const;
};

Delegate:.cpp文件

PreviewLeftWinItemDelegate::PreviewLeftWinItemDelegate(QObject* parent /*= 0*/)
{}PreviewLeftWinItemDelegate::~PreviewLeftWinItemDelegate()
{}void PreviewLeftWinItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{//自己利用项来绘制
}bool PreviewLeftWinItemDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
{return QStyledItemDelegate::editorEvent(event,model,option,index);
}QWidget* PreviewLeftWinItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{return NULL;//return QStyledItemDelegate::createEditor(parent, option, index);
}void PreviewLeftWinItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{}QSize PreviewLeftWinItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{return QSize(0, PreviewLeftWinItemHeight);
}QRect PreviewLeftWinItemDelegate::GetimageRect(QRect all_rect,QSize ori_size) const
{
//算出宽高比float rate = (float)ori_size.width() / ori_size.height();float now_rect_rate = (float)all_rect.width() / all_rect.height();QSize need_size = QSize();//左上角点int x_pos = 0;int y_pos = 0;if (rate >= now_rect_rate){//宽图;图片宽比大于容器宽比,则图片宽设置为容器宽,再求图片高int image_width = all_rect.width();int image_height = image_width / rate;need_size = QSize(image_width, image_height);//算下新左上角点x_pos = all_rect.topLeft().x();y_pos = all_rect.topLeft().y() + (all_rect.height() - image_height) / 2;}else{//高图;图片宽比小于容器宽比,则图片高设置为容器高,再求图片宽int image_height = all_rect.height();int image_width = image_height * rate;need_size = QSize(image_width, image_height);//算下新左上角点x_pos = all_rect.topLeft().x() + (all_rect.width() - image_width) / 2;y_pos = all_rect.topLeft().y();}//转换为rect内部矩形QRect need_rect = QRect(QPoint(x_pos, y_pos), need_size);return need_rect;
}

未完待续。。。

这些只是一般项目基础用法,后续还有一个模型代理需要讲一下

(Qt项视图Demo)封装下链式结构(QListView)的MVC(MVD)用法相关推荐

  1. 数据结构(顺序结构、链式结构、索引结构、散列结构)

    文章目录 1.概述 2.数据间逻辑关系 3.数据的存储结构(或物理结构) 3.1顺序结构 3.2链式结构 3.3索引结构 3.4散列结构 4.运算结构 1.概述 数据结构,就是一种程序设计优化的方法论 ...

  2. 哈夫曼树--链式结构(建立huffman树、编码、解码)

    引子: 用链式结构来建立哈夫曼树. 总体思路是: 将每一棵树(这是每一棵树只有一个节点,头节点)放在一个优先队列中(下面的是链表实现的优先队列),频率(rate)小的在队列头,取出队列头两个,频率相加 ...

  3. java链式结构_java语言实现队列顺序结构与链式结构

    本文主要向大家介绍了java语言实现队列顺序结构与链式结构,通过具体的内容向大家展示,希望对大家学习java语言有所帮助. 队列的顺序存储结构实现 public class Queue{ privat ...

  4. 区块链 - 牵一发而动全身的链式结构

    文章目录 前言 区块的结构 区块如何链接的 反向链接 分叉 如果区块中交易被篡改后 结束语 前言 我们已经深度剖析了工作量证明和梅克尔树这两个典型的哈希应用,这次我们讨论哈希的第三个应用,也是数字货币 ...

  5. 理解区块链之“链式结构”

    区块链有两个明显特点,一个是区块,另一个就是"链",即"链式结构".在"链式结构"下,它衍生出:链/Chain.链上/On-chain.链下 ...

  6. 【C】二叉树--顺序结构(详解堆的实现,topK排序、堆排)、和链式结构(链式结构的遍历、链式结构常见递归操作以及练习题)

    本章我们将引入树的概念并详细介绍二叉树.我们会在介绍顺序二叉树基础上,进一步介绍堆以及堆的实现,并以此为依据详解topK排序.堆排等问题:然后我们会介绍链式二叉树的实现以及各种操作.最后,我们也会给出 ...

  7. C/C++实现栈的链式结构(链栈)

    今天我们学习的是链栈,也就是说栈的链式结构,我们运用顺序链的方式来实现.首先呢,链栈是不存在存储空间满的情况的,所以可以说它是个无底洞,然而我们之前学的顺序栈是有额定空间的. 栈顶指针总是指向栈顶前面 ...

  8. 顺序、链式结构的个人书籍管理系统(C++)

    仅供课外学习使用,任何个人与机构不得利用此文章进行任何形式的作弊. 因上一作浏览量激增,不胜惶恐.故上传新版,恳请各路大神赐教. 个人书籍管理系统的编写.制作与实现(C语言) 项目要求 设计一个图书管 ...

  9. 二叉树链式结构的实现

    目录 1.二叉树链式结构的实现 1.1前置说明 1.2二叉树的遍历 1.2.1前序.中序以及后序遍历 1.2.2层序遍历 1.3二叉树节点个数及高度等 1.4二叉树的创建和销毁 1.5完全二叉树的判断 ...

最新文章

  1. C语言-线性表基本操作之单链表
  2. 编程笔试(解析及代码实现):猴子吃桃。猴子第一天吃了若干个桃子,当即吃了一半,还不解馋,又多吃了一个…的C++、Java、Python、C#等语言代码实现
  3. Linux (redhat 6.5)服务器上安装Webmin
  4. linux重定向文件不存在,shell 12 21 filename重定向的含义和区别
  5. OpenCV C++ 06 - Histogram Equalization of a Grayscale image with OpenCV
  6. SQL注入_1-6_user-agent注入
  7. Response笔记
  8. java 布隆过滤器_牛逼哄哄的布隆过滤器,到底有什么用?
  9. begin backup导致的故障恢复全过程
  10. android能用svg格式,Android加载html中svg格式图片进行显示
  11. 数据结构 队列Queue
  12. 向量数量积公式_向量的数量积公式大全
  13. 计算机对比度亮度调整,显示器亮度对比度调多少眼睛舒服_亮度和对比度正确调整方法...
  14. a113 智能音箱芯片方案_智能音箱九大芯片方案商及其生产厂商和代表作品介绍...
  15. 对比苏州和杭州这两座城市,你会发现?
  16. 《考研公共课复习指导》数学篇1:考研数学策略
  17. HCIP-Routing Switching V2.5考试大纲
  18. 重磅官宣:说一件大事
  19. Linux性能分析工具perf基础使用介绍
  20. 【vue3】6 vue3.0-5 生命周期

热门文章

  1. 李家同《让高墙倒下吧》
  2. Woaw Gallery中环和湾仔空间呈献国际艺术家群展
  3. stm32入门学什么板子_“蓝桥杯”嵌入式stm32开发入门(1)概述
  4. pta 藏头诗(C语言实现)
  5. 收银机打印数据截取_既然没有苹果系统,那收银机选安卓还是Windows好?
  6. 当程序员遇到会写代码的产品经理......
  7. 因成本上升,XILINX预计从2023年开始上调部分器件的价格
  8. “脸萌”一夜爆红 幕后APP开发团队大揭秘
  9. 丛林战争游戏数据库设计
  10. 再议IIC协议与设计【3】 --SCCB总线介绍