QTableView-mode中嵌入复选框CheckBox

在这里插入图片描述

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


在iOS,如果你想要在UITableView添加一列复选框,你可以使用`UITableViewCell`的`accessoryType`属性来控制单元格的附加视图。具体步骤如下: 1. 首先,在你的`UITableViewDataSource`协议,你需要返回一个`UITableViewCellStyle`枚举值,通常选择`UITableViewCellStyleValue1`,因为这包含了检查标记的位置。 ```swift func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CheckboxCell", for: indexPath) cell.accessoryType = .checkmark // 添加复选框 // 其他配置如cell的内容... return cell } ``` 2. 确保你已经创建了一个包含复选框样式的单元格(例如,你可以自定义一个`UITableViewCell subclass`),并在故事板、XIB文件或者代码注册它: ```swift let reuseIdentifier = "CheckboxCell" if let nib = UINib(nibName: reuseIdentifier, bundle: nil) { tableView.register(nib, forCellReuseIdentifier: reuseIdentifier) } else { let cell = CheckboxTableViewCell(style: .value1, reuseIdentifier: reuseIdentifier) tableView.register(cell.classForCoder, forCellReuseIdentifier: reuseIdentifier) } // CheckboxTableViewCell 需要继承自 UITableViewCell 并实现相应的复选框处理逻辑 class CheckboxTableViewCell: UITableViewCell { // ... } ``` 3. 如果需要处理用户点击复选框的行为,可以在`tableView(_:didSelectRowAt:)`代理方法实现: ```swift func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) as? CheckboxTableViewCell { cell.accessoryType = cell.accessoryType == .checkmark ? .none : .checkmark // 切换复选状态 // 更新数据源并保存状态 // ... } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值