Made QTableView and derivative from QAbstractTableModel.

There are several columns in the model - bools. I want to edit them (switch) by clicking (or double-clicking) on ​​the corresponding cell.

I can’t figure out how I don’t create any editor'es, but immediately in Delegate :: createEditor () do setModelData ().

    2 answers 2

    The essence of the delegate lies in the delegation of rights to edit model data to a separate widget. If a delegate is used and cell modification is supported, then the corresponding editor widget must be created.

    In some cases, for example, when using switches, this may seem redundant, however, it is often important to provide the ability to cancel the user's gestures in order not to disturb the model once more with random clicks with or without clicks.

    If the task is to go your own way, different from the generally accepted one, and change the values ​​in the cells for the click event, then it is probably better to stop using delegates altogether.

    In QTableView override mouse events or connect to QItemSelectionModel signals, which are available via QAbstractItemView::selectionModel() . And then, based on the input data about the cell in which the mouse was clicked, change the corresponding data in the model.

      The task, as it turned out, has the simplest solution: QTableView (like QAbstractItemView , however) has a clicked() signal.

      Then everything is simple:

       public slot void MyModel::itemClicked (const QModelIndex &index) { // some magic } .... // в конструкторе всего этого безобразия connect (ui->table, &QTableView::clicked, model, &MyModel::itemClicked);