Qt 设置QTableView的某列或某行只读

我的做法是实现一个委托(delegate),然后把它设给你要只读的列或行

cpp 复制代码
class ReadOnlyDelegate: public QItemDelegate
{
 
public:
    ReadOnlyDelegate(QWidget *parent = NULL):QItemDelegate(parent)
    {}
 
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override //final
    {
        Q_UNUSED(parent)
        Q_UNUSED(option)
        Q_UNUSED(index)
        return NULL;
    }
};

就是不返回任何editor。

然后去设置TableView对象的属性如下:

cpp 复制代码
ReadOnlyDelegate* readOnlyDelegate = new ReadOnlyDelegate();
ui->tableView->setItemDelegateForColumn(2, readOnlyDelegate); //设置某列只读
ui->tableView->setItemDelegateForRow(0, readOnlyDelegate);    //设置某行只读```

如果要将整个TableView设为只读模式,则只需下面一句即可:

cpp 复制代码
 ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
相关推荐
吃好睡好便好17 分钟前
提取矩阵某一行或某一列元素
开发语言·人工智能·线性代数·算法·matlab·矩阵
deepin_sir3 小时前
10 - 函数
开发语言·python
z落落4 小时前
C#String字符串
开发语言·c#·php
猫头虎-前端技术4 小时前
JS 作用域与闭包:从变量提升到闭包陷阱的超详细解析
开发语言·javascript·云计算·bootstrap·ecmascript·openstack·perl
枫叶林FYL4 小时前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
繁华落尽,倾城殇?4 小时前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete
01_ice5 小时前
C语言数据在内存中的存储
c语言·开发语言
代码村新手5 小时前
C++-二叉搜索树
开发语言·c++
吃好睡好便好6 小时前
创建魔方矩阵和单位矩阵
开发语言·人工智能·学习·线性代数·matlab·矩阵
影寂ldy6 小时前
C#数组的属性和方法(Clear / Copy / IndexOf )
开发语言·javascript·c#