QT6 源(135)QItemSelectionRange:本类描述了模型里被连续选择的条目组成的集合,以及源代码带注释

(1) 本类定义于头文件 qitemselectionmodel . h

/*
The QItemSelectionRange class manages information about a
    range of selected items in a model.
QltemSelectionRange 类管理模型中选定项目范围的信息。

Detailed Description :
QltemSelectionRange`包含关于模型中选定项范围的信息。
项范围是模型项的连续数组,延伸以覆盖具有共同父项的多个相邻行和列;
这可以可视化为一个表格中的二维单元块。
一个选择范围具有`top()、right()'和一个`parent()、left()、bottom().

QItemSelectionRange类是模型/视图类之一,是Qt模型/视图框架的一部分。
通过使用`indexes()、函数,可以获取选定的范围中所包含的模型项。
使用QItemSelectionModel::selectedIndexes()、可获取视图中所有选中的项的列表。

您可以通过使用“ contains() ”函数来确定给定的模型项是否位于特定范围内。
还可以使用重载的等于和不等于运算符来比较范围,而“ intersects() 函数则允许您确定两个范围是否重叠。


*/

class Q_CORE_EXPORT QItemSelectionRange
{   //本类描述的是这样的选择项:这些被选择的条目都是连续的。
private: //tl : top left ,  br  : bottom right 的含义
    QPersistentModelIndex tl, br; //使用了两个持久索引作为自己的数据成员

public:
    QItemSelectionRange() = default; //默认构造函数

    QItemSelectionRange(const QModelIndex & topL, const QModelIndex & bottomR)//有参构造函数
        : tl(topL), br(bottomR) {}

    explicit
    QItemSelectionRange(const QModelIndex & index) : tl(index), br(tl) {}     //有参构造函数

    inline bool operator==(const QItemSelectionRange & other) const //比较运算符函数
    {   return (tl == other.tl && br == other.br); }

    inline bool operator!=(const QItemSelectionRange & other) const
    {   return !operator==(other); }


    void swap(QItemSelectionRange & other) noexcept
    {
        tl.swap(other.tl);
        br.swap(other.br);
    }


    inline const QPersistentModelIndex &     topLeft() const { return tl; }
    inline const QPersistentModelIndex & bottomRight() const { return br; }

    inline int    top() const { return tl.row   (); }
    inline int   left() const { return tl.column(); }
    inline int bottom() const { return br.row   (); }
    inline int right () const { return br.column(); }

    inline int  width() const { return br.column() - tl.column() + 1; }
    inline int height() const { return br.row()    - tl.row()    + 1; }

    //Returns true if the selection range is valid; otherwise returns false.
    inline
    bool isValid() const //若本选择范围有效,则返回 true。
    {
        return (
            tl   .isValid()
            && br.isValid()
            && tl.parent() == br.parent()
            && top () <= bottom()
            && left() <= right());
    }

    bool isEmpty() const;
    //如果选择范围包含没有项目或仅包含禁用或标记为不可选的项目,则返回true。

    //Returns the model that the items in the selection range belong to.
    inline const QAbstractItemModel * model  () const { return tl.model (); }

    inline       QModelIndex          parent () const { return tl.parent(); }
    //Returns the parent model item index of the items in the selection range.
    //返回这些被选条目的父节点的索引。

    QModelIndexList                   indexes() const;
    //Returns the list of model index items stored in the selection.

    //判断本选择范围里是否包含形参 index指向的条目。
    inline bool contains(                     const QModelIndex & index) const
    {
        return (
                parent() == index.parent()
                && tl.row() <= index.row() && tl.column() <= index.column()
                && br.row() >= index.row() && br.column() >= index.column()
            );
    }

    //判断节点 parentIndex下属的子表里元素 [row, column]是否在本选择范围内。
    inline bool contains(int row, int column, const QModelIndex & parentIndex) const
    {
        return (
                parent() == parentIndex
                && tl.row() <= row && tl.column() <= column
                && br.row() >= row && br.column() >= column
            );
    }

    //如果此选择范围与给定的 other范围相交(重叠),则返回true;否则返回false。
    bool                intersects (const QItemSelectionRange & other) const;
    QItemSelectionRange intersected(const QItemSelectionRange & other) const;
    //返回一个新的选择范围,仅包含在选定范围和 other选定范围内都找到的项目。

}; //完结 class Q_CORE_EXPORT ItemSelectionRange
Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_RELOCATABLE_TYPE);

Q_CORE_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
//此 << 运算符函数说明本类支持调试打印输出。

(2)

谢谢

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值