How to allow editing by (submit) for only one column of a table?

setTable("orders"); setRelation(fieldIndex("worker_id"),QSqlRelation("workers","worker_id","name")); setEditStrategy(QSqlRelationalTableModel::OnManualSubmit); select(); ui->tableView->setModel(model); ui->tableView->resizeColumnsToContents(); ui->tableView->hideColumn(0); ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tableView->setItemDelegateForColumn(model->fieldIndex("worker_id"),new QSqlRelationalDelegate(ui->tableView)); 

at the moment the connections are working. Editing is not possible. If I put AllEditTriggers, then all fields can be edited. From worker_is "combobox" does not fall out (((but if I put an ID there, then it pulls the name. Ps how to allow editing only one column worker_id.

    2 answers 2

    You can inherit your model and override the flags() method:

     class Model : public QSqlTableModel { Q_OBJECT Model(QObject *parent = Q_NULLPTR) : QSqlTableModel(parent) {} Qt::ItemFlags Model::flags(const QModelIndex &index) const { if(index.column() == fieldIndex("worker_id")) { return QSqlQueryModel::flags(index) | Qt::ItemIsEditable; } return QSqlQueryModel::flags(index); } } 

    You can create a delegate and set it in the widget view:

     class Delegate : public QStyledItemDelegate { Q_OBJECT public: Delegate(QObject *parent = Q_NULLPTR) : QStyledItemDelegate(parent) {} virtual QWidget *createEditor(QWidget *parent , const QStyleOptionViewItem &option , const QModelIndex &index) const { const QSqlTableModel *model = qobject_cast<const QSqlTableModel*>(index.model()); if(model != Q_NULLPTR) { if(index.column() == model->fieldIndex("worker_id")) return Q_NULLPTR; } return QStyledItemDelegate ::createEditor(parent, option, index); } }; 
    • is there a possibility of combining dubleclicked work with all this (deligat) ??? - Alexey Smirnov
    • I do not understand the question. - alexis031182
    • Well, I set the slot on the tableView to open the window for editing by double clicking on_tableView_doubleClicked, and the checkbox also opens if you double-click on the cell ( - Alexey Smirnov
    • Behavior on the activation of editing can be controlled through this method . - alexis031182

    crmmodel.h (further refractoring-> virtual methods-> flags ())

     class CRMModel : public QSqlRelationalTableModel { Q_OBJECT public: CRMModel(QObject *parent = Q_NULLPTR, QSqlDatabase db = QSqlDatabase()); private: int m_value; public: Qt::ItemFlags flags(const QModelIndex &index) const; }; 

    crmmodel.cpp

     CRMModel::CRMModel(QObject *parent, QSqlDatabase db) : QSqlRelationalTableModel(parent, db) { setTable("orders"); setRelation(fieldIndex("worker_id"),QSqlRelation("workers","worker_id","name")); setEditStrategy(QSqlRelationalTableModel::OnManualSubmit); select(); } Qt::ItemFlags CRMModel::flags(const QModelIndex &index) const { if(index.column() == fieldIndex("name")) { return QSqlQueryModel::flags(index)| Qt::ItemIsEditable; } return QSqlQueryModel::flags(index); } 

    mainwindow.cpp (a piece of code)

     model = new CRMModel(this,baze); ui->tableView->setModel(model); ui->tableView->resizeColumnsToContents(); ui->tableView->hideColumn(0); // ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tableView->setItemDelegateForColumn(model->fieldIndex("worker_id"),new QSqlRelationalDelegate(ui->tableView)); 

    starts up. there are no errors. current so that you can edit it is necessary to disable the doubleClicked slot. (I have a window for editing opened by double clicking).