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; }
188:比循环还麻烦
2024-02-29 13:30:27 回复