QTableView中嵌入复选框CheckBox

来源:互联网 发布:软件咨询服务 编辑:程序博客网 时间:2024/05/22 06:44

QTableView中嵌入复选框CheckBox

设置QAbstractTableModel的flags()函数法

通过Delegate创建QCheckBox来实现的Check列,只有在该列进入编辑模式时才能够Check/Uncheck。这显然不是我们想要的,网上翻来翻去,在一个国外论坛中看到了无需Delegate的实现方法,只需重写Model即可:

主要是修改两个函数:

//设置某一列为可选角色,绘画出QCheckBox

Qt::ItemFlags flags(const QModelIndex &index) const;

//根据界面选择QCheckbox,修改Model中的数据

bool setData(const QModelIndex &index, const QVariant &value, int role);

1. 2.在StudentInfoModel .h头文件中的主要代码:  2. class StudentInfoModel : public QAbstractTableModel   3. {  4.     Q_OBJECT  5. public:  6.     StudentInfoModel(const int totalColumn, const int aColumnNumWithChechBox = 0, QObject *parent = 0)  7.     :totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox),  8.       9.     QAbstractTableModel(parent) {rowCheckStateMap.clear();};  10. public:  11.     int rowCount(const QModelIndex &parent = QModelIndex()) const;  12.     int columnCount(const QModelIndex &parent = QModelIndex()) const;  13.     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;  14.     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;  15.     Qt::ItemFlags flags(const QModelIndex &index) const;  16.     bool setData(const QModelIndex &index, const QVariant &value, int role);  17.   18.   19. public:  20.     void AddStudentInfo(const StudentInfo &studentInfo);  21.   22.   23.     signals:  24.     void StudentInfoIsChecked(const StudentInfo &studentInfo);  25.   26.   27. private:  28.     typedef QVector<StudentInfo> StudentInfos;  29.     StudentInfos studentInfos;  30.     int totalColumn;  31.     int colNumberWithCheckBox;  32.     QMap<int, Qt::CheckState> rowCheckStateMap;  33. };  
1. 3.在StudentInfoModel.cpp文件中的主要代码如下:  2. QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const  3. {  4.     if (role == Qt::DisplayRole)   5.     {   6.         if (index.column() == 0)   7.             return QString::number(index.row()+1);   8.         if (index.column() == 1)   9.             return studentInfos[index.row()].stuNumber;   10.         if (index.column() == 2)  11.             return studentInfos[index.row()].stuName;   12.         if (index.column() == 3)  13.             return studentInfos[index.row()].stuID;   14.         if (index.column() == 4)  15.             return studentInfos[index.row()].stuPhoneNumber;  16.         if (index.column() == 5)   17.             return studentInfos[index.row()].department;   18.         if (index.column() == 6)   19.             return studentInfos[index.row()].stuDescription;   20.     }   21.     if (role == Qt::CheckStateRole)   22.     {   23.         if (index.column() == colNumberWithCheckBox)   24.         {   25.             if (rowCheckStateMap.contains(index.row()))   26.             return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked;   27.         }   28.     }   29.     return QVariant();  30. }  31.   32.   33. Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const  34. {  35.     if  36.     (!index.isValid())  37.     return 0;  38.   39.   40.     if (index.column() == colNumberWithCheckBox)  41.     return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;  42.   43.   44.     return  Qt::ItemIsEnabled | Qt::ItemIsSelectable;  45. }  46.   47.   48. bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role )  49. {  50.     if(!index.isValid())  51.         return false;  52.     if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)  53.     {  54.         if (value == Qt::Checked) //  55.         {  56.             rowCheckStateMap[index.row()] = Qt::Checked;   57.             if(studentInfos.size() > index.row())  58.             emit StudentInfoIsChecked(studentInfos[index.row()]);  59.         }  60.         else  61.         {  62.             rowCheckStateMap[index.row()] = Qt::Unchecked;  63.         }   64.     }  65.     return true;  66. }   

【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结

QTableView中嵌入复选框实例