How to make only certain cells in tableview editable
-
this->setEditTriggers(QAbstractItemView::NoEditTriggers | QAbstractItemView::DoubleClicked);
I am wondering how to make only certain cells in tableview editable. For now, I use setEditTriggers to make it editable.
The problem is that all cells are editable.
But I want to make only certain cells editable.
How can I do that? -
self-response
QWidget* usrTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
//QWidget* w = QStyledItemDelegate::createEditor(parent, option, index);
//QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);usrSetupLineEdit* pEdit = new usrSetupLineEdit(parent);//qobject_cast<usrSetupLineEdit*>(lineEdit); pEdit->SetOnlyNumber(true, true); pEdit->SetRange(-4.0, 4.0); pEdit->setAlignment(Qt::AlignCenter); return pEdit;
}
void usrTableDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
} -
void wStandardCurve::CellDoubleClicked(const QModelIndex& index) { if (index.column() == 0) { //index.data().isE QStandardItem* cast = m_pDataModel->item(index.row(), index.column()); usrStandardItem* item = static_cast<usrStandardItem*>(cast); item->setFlags(item->flags() | Qt::ItemIsEditable); item->setEditable(true); item->isEditable(); //QString str = item->text(); QString str = index.data().toString(); } else if (index.column() == 2) { QStandardItem* cast = m_pDataModel->item(index.row(), index.column()); usrStandardItem* item = static_cast<usrStandardItem*>(cast); item->setFlags(Qt::ItemIsEditable); item->setEditable(true); item->isEditable(); QString str = item->text(); //QString str = index.data().toString(); } }
I tried this way but couldn't get the result I want.
-
If particular items in your model should not be (or are not) editable in any view then the items in the model should have the default Qt::ItemIsEditable flag removed (not added as you attempt above). You can do this calling QStandardItem::setEditable(), or through the model interface with setFlags().
If the items in the model are generally editable, but should be read only in a particular view only, then you can approach this by using a QAbstractProxyModel that changes the base model flags on selected items..
Base model <=> Proxy model <=> View
-
QWidget* usrTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QWidget* w = QStyledItemDelegate::createEditor(parent, option, index); //QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w); usrSetupLineEdit* pEdit = new usrSetupLineEdit(w);//qobject_cast<usrSetupLineEdit*>(lineEdit); pEdit->SetOnlyNumber(true, true); pEdit->SetRange(-4.0, 4.0); pEdit->setAlignment(Qt::AlignCenter); return pEdit; } I'll ask you one more thing When double-clicking, I want to show the custom control I made. The result does not happen inside the table cell, but outside the table cell. How do I put it into a table?
-
self-response
QWidget* usrTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
//QWidget* w = QStyledItemDelegate::createEditor(parent, option, index);
//QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);usrSetupLineEdit* pEdit = new usrSetupLineEdit(parent);//qobject_cast<usrSetupLineEdit*>(lineEdit); pEdit->SetOnlyNumber(true, true); pEdit->SetRange(-4.0, 4.0); pEdit->setAlignment(Qt::AlignCenter); return pEdit;
}
void usrTableDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
}