csdn一大片都是ui->tableWidget->item(num,0)->setFlags(Qt::NoItemFlags);
这明显是某行某列,要一增列还要搞个for循环,一个字,挫!
这是解决方案,适用于tableview:// 设置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;
}
本文为3YL原创,转载无需联系,但请注明来自labisart.com。
原创文章不易,如果觉得有帮助,可打赏或点击右侧广告支持: