QTableView中嵌入复选框CheckBox
2016-03-22 16:50
441 查看
第二种方法:设置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);
Cpp代码
2.在StudentInfoModel .h头文件中的主要代码:
class StudentInfoModel : public QAbstractTableModel
{
Q_OBJECT
public:
StudentInfoModel(const int totalColumn, const int aColumnNumWithChechBox = 0, QObject *parent = 0)
:totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox),
QAbstractTableModel(parent) {rowCheckStateMap.clear();};
public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
public:
void AddStudentInfo(const StudentInfo &studentInfo);
signals:
void StudentInfoIsChecked(const StudentInfo &studentInfo);
private:
typedef QVector<StudentInfo> StudentInfos;
StudentInfos studentInfos;
int totalColumn;
int colNumberWithCheckBox;
QMap<int, Qt::CheckState> rowCheckStateMap;
};
3.在StudentInfoModel.cpp文件中的主要代码如下:
QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const
{
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
return QString::number(index.row()+1);
if (index.column() == 1)
return studentInfos[index.row()].stuNumber;
if (index.column() == 2)
return studentInfos[index.row()].stuName;
if (index.column() == 3)
return studentInfos[index.row()].stuID;
if (index.column() == 4)
return studentInfos[index.row()].stuPhoneNumber;
if (index.column() == 5)
return studentInfos[index.row()].department;
if (index.column() == 6)
return studentInfos[index.row()].stuDescription;
}
if (role == Qt::CheckStateRole)
{
if (index.column() == colNumberWithCheckBox)
{
if (rowCheckStateMap.contains(index.row()))
return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked;
}
}
return QVariant();
}
Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const
{
if
(!index.isValid())
return 0;
if (index.column() == colNumberWithCheckBox)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if(!index.isValid())
return false;
if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)
{
if (value == Qt::Checked) //
{
rowCheckStateMap[index.row()] = Qt::Checked;
if(studentInfos.size() > index.row())
emit StudentInfoIsChecked(studentInfos[index.row()]);
}
else
{
rowCheckStateMap[index.row()] = Qt::Unchecked;
}
}
return true;
}
通过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);
Cpp代码
2.在StudentInfoModel .h头文件中的主要代码:
class StudentInfoModel : public QAbstractTableModel
{
Q_OBJECT
public:
StudentInfoModel(const int totalColumn, const int aColumnNumWithChechBox = 0, QObject *parent = 0)
:totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox),
QAbstractTableModel(parent) {rowCheckStateMap.clear();};
public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
public:
void AddStudentInfo(const StudentInfo &studentInfo);
signals:
void StudentInfoIsChecked(const StudentInfo &studentInfo);
private:
typedef QVector<StudentInfo> StudentInfos;
StudentInfos studentInfos;
int totalColumn;
int colNumberWithCheckBox;
QMap<int, Qt::CheckState> rowCheckStateMap;
};
3.在StudentInfoModel.cpp文件中的主要代码如下:
QVariant StudentInfoModel::data( const QModelIndex &index, int role ) const
{
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
return QString::number(index.row()+1);
if (index.column() == 1)
return studentInfos[index.row()].stuNumber;
if (index.column() == 2)
return studentInfos[index.row()].stuName;
if (index.column() == 3)
return studentInfos[index.row()].stuID;
if (index.column() == 4)
return studentInfos[index.row()].stuPhoneNumber;
if (index.column() == 5)
return studentInfos[index.row()].department;
if (index.column() == 6)
return studentInfos[index.row()].stuDescription;
}
if (role == Qt::CheckStateRole)
{
if (index.column() == colNumberWithCheckBox)
{
if (rowCheckStateMap.contains(index.row()))
return rowCheckStateMap[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked; return Qt::Unchecked;
}
}
return QVariant();
}
Qt::ItemFlags StudentInfoModel::flags( const QModelIndex &index ) const
{
if
(!index.isValid())
return 0;
if (index.column() == colNumberWithCheckBox)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
bool StudentInfoModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if(!index.isValid())
return false;
if (role == Qt::CheckStateRole && index.column() == colNumberWithCheckBox)
{
if (value == Qt::Checked) //
{
rowCheckStateMap[index.row()] = Qt::Checked;
if(studentInfos.size() > index.row())
emit StudentInfoIsChecked(studentInfos[index.row()]);
}
else
{
rowCheckStateMap[index.row()] = Qt::Unchecked;
}
}
return true;
}
相关文章推荐
- 关于QT的几个问题
- Qt实现网络数据传输 HttpClient
- VS2015中使用QT5.6.0
- Windows环境QtCharts安装配置说明
- qt 自定义菜单
- qt 编写插件 很好的解释了 WorkStation.exe 和trade.dll 之间如何调用
- QT QTableView 获取某行的所有信息
- Qt学习之解决QListWidgetItem有重叠时,点一下全部选中的问题
- Qt SQLite数据库操作
- qt 拖动 讲解 QDrag
- Qt 5.6 的 Qt.labs.controls 在安卓上效果
- Qt5自带加密方法
- QTableview的一些用法
- Qt中如何添加ICON图标 Qt设置应用程序图标
- Qt官方开发环境生成的exe发布方式--使用windeployqt
- winpcap在VS2012 Qt5 X64下的配置
- QT - Model/View 总结
- QT信号与槽的自动连接
- Qt之qSort
- QT布局管理器(QVBoxLayout,QHBoxLayout)不同部分比例大小设置方法