[solved] How to disable cells in QTableView
-
Dear all,
I have QTableView which created using QStandardItemModel.
I also create QSpinBox delegate to edit the cells data.
But I have to disable some SpinBoxes to avoid change value by user.
So my problem is how to disable some cells of QTabelView.
I find some were recommended to use
@tableView.setEditTriggers(QAbstractItemView::NoEditTriggers);@But it disable entire TableView.
-
You can use
@void QStandardItem::setFlags(Qt::ItemFlags flags)@
for that. "Flags":http://qt-project.org/doc/qt-5/qt.html#ItemFlag-enum . -
qxoz,
Thank you very much for your helping hand. Your solution is working fine.
I used below code.
@
for (int row = 0; row < rows().count(); ++row){
for (int col = 0; col < cols().count(); ++col){QModelIndex index = item_model->index(row,col,QModelIndex()); if(toBeDisabled()){ QStandardItem *my_item = item_model->itemFromIndex(index); my_item->setFlags(!Qt::ItemIsEditable); } else{ table_view->openPersistentEditor(index); } item_model->setData(index,0); }
}@
Thanks again.
-
@qxoz said in [solved] How to disable cells in QTableView:
You can use
@void QStandardItem::setFlags(Qt::ItemFlags flags)@
for that. "Flags":http://qt-project.org/doc/qt-5/qt.html#ItemFlag-enum .How do you write this code in PyQt ? I wish I could translate Qt to PyQt code by just looking at it, but I can't.
-
model.item(row,column).setFlags(Qt.NoItemFlags)
I wish I could translate Qt to PyQt code
As a general rule if there is a symbol you don't understand replace it with
.
and it works :) -
@Echeban
I see that the OP, @Hareen-Laks , wrote as his own solution 5 years ago:my_item->setFlags(!Qt::ItemIsEditable);
If you're thinking of translating that into Python/PyQt, I wouldn't. 'coz it's wrong ;-) (Compiles but isn't the right code.)