Qt的model中一裤衩函数的总结

写了两年Qt程序,翻看这篇博文,菜鸡的不忍直视。如果你点开了,时间又不是很充裕的话,别看了。

最近要用Qt做一个MVC的项目。一打开model,我擦晕了。满眼的函数,完全不知道怎么用。和以往猜一猜加随便看两眼文档就开始大刀阔斧的使用Qt不同。啃了两天,终于肯掉了肉,肯出了骨头。闲话少说,毕竟上班时间写博客也是划水的一种。


Qt的model中包含了很多名字看起来很像的函数,比如下面这组:

bool    insertRow(int row, const QModelIndex & parent = QModelIndex())
bool    insertRows(int row, int count, const QModelIndex & parent = QModelIndex())
void    beginInsertRows(const QModelIndex & parent, int first, int last)
void    endInsertRows()
void    rowsAboutToBeInserted(const QModelIndex & parent, int start, int end)
void    rowsInserted(const QModelIndex & parent, int start, int end)

整理了很多函数以后,发现可以这样归纳。设操作为Opera,方向为Orientation.那么Qt这些函数可以归纳为如下的几种

                  bool    operaOrientation
QVariant  bool    operaOrientations
                  void    beginOperaOrientations
                  void    endOperaOrientations
                  void    OrientationsAboutToBeOperaed
                  void    OrientationsOperaed

其中Opera包括插入insert,删除remove,移动move,重置reset。Orientation包括行row和列colum。

比如Opera为insert,Orientation为row ,operaOrientation就是insertRow。

这么多函数,他们到底怎么用的呢?在model中的数据有改变的时候,这些函数到底起什么作用?如我一一道来。

首先说operaOrientations,operaOrientations是个虚函数,我们在重定义的operaOrientations函数中,对model的数据进行opera。比如删除行,那么就重新实现removeColuns就可以了。

如过只是实现了operaOrientations就完成了,那么其他函数存在的意义是什么呢?做为有求知欲的我们肯定要一探究竟的。

其中operaOrientation是operaOrientations的一种包装,也就是说operaOrientation最后还是调用了operaOrientations完成。这也是为什么opera多个要重实现operaOrientations,opera一个却不用重实现operaOrientation的原因。

现在提出两个问题:view操作数据时候,model却把数据给改变了怎么办?model的数据改变了,View不知道怎么办?说白了就是model的数据和View是如何同步的?

这个时候剩下的4个函数就起作用了。

我们在operaOrientations中真正的opera之前必须使用beginOperaOrientations,此时,model会发射OrientationsAboutToBeOperaed信号,opera之后还要endOperaOrientations,此时model会发射OrientationsOperaed信号。


研究了两天,发现这样使用基本可以了,但是还有一个遗留的疑问。

在opera中为什么没有编辑。setItemData函数中为什么不使用beginSetOrientations,endSetOrientations并发射信号呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值