`
qimo601
  • 浏览: 3462634 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

(二)Qt中QTableView中加入Check列实现

    博客分类:
  • Qt
阅读更多
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);

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;
}
 
 
 

  • 大小: 22.1 KB
分享到:
评论
2 楼 a2865285 2014-05-25  
a2865285 写道
构造函数 StudentInfoModel 下面的是什么啊

:totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox), 
     
    QAbstractTableModel(parent) {rowCheckStateMap.clear();}; 


哦 我去 看出来了 初始化参数列表....
1 楼 a2865285 2014-05-25  
构造函数 StudentInfoModel 下面的是什么啊

:totalColumn(totalColumn),colNumberWithCheckBox(aColumnNumWithChechBox), 
     
    QAbstractTableModel(parent) {rowCheckStateMap.clear();}; 

相关推荐

    QTableView显示自定义CheckBox

    在Qt框架中,`QTableView`是一个非常常用的控件,用于展示二维表格数据。而有时候,我们可能需要在表格的某一列中显示可选的复选框(CheckBox),以供用户进行选择操作。本篇文章将深入讲解如何在`QTableView`中实现...

    QTableView插入QCheckBox复选框

    在Qt框架中,QTableView是一种常用的控件,用于展示二维数据表格。QTableView与QTableWidget相比,更灵活且高效,适用于大数据量的显示。在实际应用中,我们经常需要在表格中添加交互元素,比如复选框,以增强用户...

    qtableview 自定义委托 checkbox,进度条、时间等控件

    在Qt框架中,QTableView是用于显示表格数据的控件,它允许用户浏览和编辑二维数据。当默认的行和列展示方式不足以满足特定需求时,我们可以通过自定义委托(QStyledItemDelegate)来扩展其功能。这个自定义委托的...

    QTableView同时显示自定义Checkbox和文件图标

    在Qt框架中,`QTableView`是一个非常常用的控件,用于展示二维表格数据。在实际应用中,我们可能需要在表格中不仅展示文本信息,还要包含其他元素,比如自定义的复选框(Checkbox)和文件图标。在描述中提到的场景中...

    QT的QTreeWidget有checkbox时轻松选中

    在QT编程中,QTreeWidget是一个非常常用的控件,它用于展示层次化的数据,并且可以进行交互操作。在QTreeWidget中添加复选框(checkbox)功能,可以让...在QT的开发过程中,灵活地定制控件的行为是实现特定功能的关键。

    QT动态增减控件代码(包含交互内容)

    在QT中,动态增减控件主要通过编程方式实现,可以使用QLayout和QWidget类来完成。QLayout是用来管理窗口小部件布局的类,它可以自动调整控件的位置和大小。而QWidget是所有窗口小部件的基类,包括按钮、文本框等。当...

Global site tag (gtag.js) - Google Analytics