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);
相关推荐
不做Java程序猿好多年3 小时前
Java中 String、StringBuffer、StringBuilder 的区别详解
开发语言·python
会周易的程序员3 小时前
js-shm: 高性能 Node.js 共享内存模块
开发语言·javascript·c++·node.js·共享内存·shm
笨蛋不要掉眼泪3 小时前
Java虚拟机:常用参数
java·开发语言·python
happy_0x3f3 小时前
前端应用的离线暂停更新策略
开发语言·前端·php
shylyly_4 小时前
C++中的类型转换
开发语言·c++·匿名对象·隐式类型转换·拷贝优化
北冥you鱼4 小时前
Go 语言新手扫盲:指针 * 和 & 使用场景详解
开发语言·后端·golang
cui_ruicheng5 小时前
Python数据分析(一):数据分析概述与环境搭建
开发语言·python·数据分析
心平气和量大福大5 小时前
C#-WPF-UserControl-生命周期(加载 退出)
开发语言·c#·wpf
sunywz5 小时前
【c#】 Web Deploy一键发布,IIS部署全流程
开发语言·前端·c#
Brilliantwxx5 小时前
【Linux】 软件包管理器(yum)+ Vim使用
linux·运维·服务器·开发语言·编辑器·vim