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);
相关推荐
码途行者6 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
头发还在的女程序员6 小时前
Java充电桩平台源码解析:枪口状态、上报时间与页面刷新怎样协同
java·开发语言·充电桩系统·充电桩管理·充电系统·充电小程序
timeDifference₇7 小时前
Python Vibe Coding实操实验报告
开发语言·jvm·python
geovindu8 小时前
python:DecisionTree
开发语言·后端·python·算法
ShineWinsu8 小时前
对于Qt:事件的解析
c++·qt·面试·笔试·gui·客户端·事件
geovindu10 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
炸膛坦客13 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
夜不会漫长13 小时前
C++:C++11特性
开发语言·c++·算法
apcipot_rain15 小时前
C++转java日记(13)
java·开发语言
郝学胜-神的一滴15 小时前
Effective Python 条款41:试Mix‑In组合式开发
开发语言·pytorch·python·pycharm·numpy