需要实现一个功能:在QT的TreeView中,能够使用复选框,并且选中父节点的复选框可以全选或取消子节点的复选框。这里就以QT附带的simpletreemodel项目为例,说明一下其用法。simpletreemodel项目的路径通常在qt目录的example目录的itemviews目录下,例如,我的就在C:/Qt/2010.05/qt/examples/itemviews里。 1.在头文件treemodel.h中,需要增加头文件 #include <QList> #include <QPersistentModelIndex> 然后在treemodel类的定义中,加入setdata函数和m_checkedList变量的定义 view plaincopy to clipboard 1 public: 2 bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ); 3 QList<QPersistentModelIndex> m_checkedList; 其中,setData是treemodel类的父类QAbstractItemModel 中定义的一个函数,它的功能是响应鼠标点击结点的动作。 m_checkedList则是用来保存被选中(复选框内打勾)的结点的信息 2.在到treemodel.cpp文件中修改。主要是修改flags()、data()两个函数,并实现setData()函数。 flags()函数修改为: view plaincopy to clipboard 4 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const 5 { 6 if (!index.isValid()) 7 return 0; 8 if (index.column()=
Qt treewidget 复选框
最新推荐文章于 2024-05-15 20:24:08 发布