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);
相关推荐
小老鼠不吃猫15 分钟前
深入浅出(十三)QWT库——高稳定二维绘图
c++·qt·二维图
下北沢美食家22 分钟前
Express框架入门
开发语言·javascript·express
遥望九龙湖33 分钟前
打包动态库
开发语言·c++·visualstudio
m0_531237171 小时前
C语言-编程实例2
c语言·开发语言
dreams_dream1 小时前
Python 的 GIL 是什么?有什么影响?
开发语言·python
麻瓜pro1 小时前
【迭代】高性能c++实时对话系统e2e_voice
开发语言·c++·onnxruntime·端到端语音
zjxtxdy1 小时前
C语言(续)
c语言·开发语言
无尽的沉默1 小时前
Thymeleaf 基本语法和表达式
java·开发语言
Coder_Boy_2 小时前
Java后端核心技术体系全解析(个人总结)
java·开发语言·spring boot·分布式·spring cloud·中间件
zh_xuan2 小时前
kotlin Flow的用法2
android·开发语言·kotlin·协程·flow·被压