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);
相关推荐
临床数据科学和人工智能兴趣组38 分钟前
在R语言中, 使用 as.factor() 函数转换数值型变量
开发语言·r语言
andyweike4 小时前
php笔记
开发语言·笔记·php
Circ.5 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
现任明教教主~6 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou1411367 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
码匠许师傅7 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http
小小小小钰儿8 小时前
1.5-Python网络编程
开发语言·网络·python·web安全·计算机·网络安全
hiahiahia1238 小时前
实现完整 Tool Dispatcher
开发语言·前端