Qtableview-How to make noneditable column?
-
Qtableview-How to make noneditable column( i have 3 qtableview in all table i need to make 2 or 3 column as non editable
noneditable is class extracting Qstandarditemmodel
Qt::ItemFlags nonedittablemodel::flags(const QModelIndex& index) const { //table 1 if (index.column() == 0 || index.column() == 1) return QStandardItemModel::flags(index) & ~Qt::ItemIsEditable; else return QStandardItemModel::flags(index) | Qt::ItemIsEditable; }
But still its not working for me
and where i should specify which model because i have 3 model 3Qtableview -
Hi,
There's no need to create a custom mode for that. You can simply set the flags on the items when you create them.
-
@SGaist said in Qtableview-How to make noneditable column?:
Hi,
There's no need to create a custom mode for that. You can simply set the flags on the items when you create them.@n-2204
I said to you at https://forum.qt.io/topic/101447/qtableview-qstandarditemmodel-and-underlying-data-in-a-qlist/12You can use @VRonin's
setFlags()
if you want to set the editability on desired, explicitly specified items.If you want to do it your way by overriding flags(), your code should have read:
...
You will have to derive fromQStandardItemModel()
if you are wanting to override itsflags()
method. Similar if you choose to useQAbstractItemModel()
instead.You showed you already had a derived model class with a
flags()
method. You can do it either way. -
You have to call the base class implementation.
-
@SGaist Qt::ItemFlags GAS_tool::flags(const QModelIndex& index) const
{
//table 1
// QStandardItemModel* model;
QAbstractItemModel* table1 = ui.tableView->model();
int iRows = table1->rowCount();
int iCols = table1->columnCount();
QModelIndex index = ui.tableView;
if (index.column() == 0)//clumn 1 should not be editable
return flags(index) & ~Qt::ItemIsEditable;
else
return flags(index) | Qt::ItemIsEditable;} like this ?
-
@n-2204 A solution that does not require override any method of the model is to use a delegate:
class ReadOnlyDelegate: public QStyledItemDelegate{ public: using QStyledItemDelegate::QStyledItemDelegate; QWidget *QStyledItemDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override { return nullptr; } };
tableView->setItemDelegateForColumn(0, new ReadOnlyDelegate); tableView->setItemDelegateForColumn(1, new ReadOnlyDelegate);
-
@n-2204 said in Qtableview-How to make noneditable column?:
so is it possible to add one more delegate to same column?
No but you can modify your delegate and add the functionality @eyllanesc showed you.
-
@Christian-Ehrlicher thanks
understood