当前位置: 首页 > article >正文

Qt 如何优雅的设置qtablewidget qtableview某列不可编辑、只读?

查找了很多文章,大多数是以下这句,但本人在代码中添加下面这句代码,并不生效!

ui->tableWidget->item(num,0)->setFlags(Qt::NoItemFlags);

于是,又找了更多的代码去尝试,尝试有效果的代码

// 设置0列只读    
    ReadOnlyDelegate* readOnlyDelegate = new ReadOnlyDelegate(this);
    ui->tableWidget->setItemDelegateForColumn(0, readOnlyDelegate);
    ui->tableWidget->setItemDelegateForColumn(1, readOnlyDelegate);
    ui->tableWidget->setItemDelegateForColumn(2, readOnlyDelegate);

其中,delegate为:

// 设置tableview某行/列不可编辑,
class ReadOnlyDelegate: public QItemDelegate
{
 
public:
    ReadOnlyDelegate(QWidget *parent = NULL):QItemDelegate(parent)
    {}
 
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const override;
};

实现为:

#include "readonlydelegate.h"
QWidget *ReadOnlyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const //final
{
    Q_UNUSED(parent)
    Q_UNUSED(option)
    Q_UNUSED(index)
    return NULL;
}

http://www.kler.cn/news/342997.html

相关文章:

  • 【Qt】控件概述(7)—— 布局管理器
  • uni-app之旅-day05-商品详情
  • 基于 Redis 实现消息队列的深入解析
  • go 语言学习路线图
  • 【Linux】自主shell编写
  • 接口自动化在业务内的应用落地
  • [linux] 在VMware中安装linux、文件下载及详细安装过程(附下载链接)
  • npm运行时出现npm ERR! builtins is not a function报错!
  • Chromium 中chrome.history扩展接口c++实现
  • 【最新华为OD机试E卷-支持在线评测】补种未成活胡杨(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)
  • kali在git外网的代理
  • 环境变量
  • 75.【C语言】文件操作(3)
  • 第 3 章:使用 Vue 脚手架
  • 【JVM】实战篇
  • 【寻找one piece的算法之路】滑动窗口
  • 【代码笔记】
  • SpringBoot3.3 优雅启停定时任务
  • Java对请求参数进行校验
  • 用Python编写一个Web爬虫:自动获取感兴趣的新闻