CustomSqlModel.cppCustomSqlModel::CustomSqlModel(QTableView **parentTable, QObject *parent) : QSqlTableModel(parent){ tableView = *parentTable;}bool CustomSqlModel::setData(const QModelIndex & index, const QVariant & value, int role){ saveSelection(); return QSqlTableModel::setData(index,value, role);} void CustomSqlModel::saveSelection(){ savedIndexList = tableView->selectionModel()->selection().indexes();}void CustomSqlModel::loadSelection(){ // добавить немного волшебства тут QItemSelection selection; foreach (QModelIndex index, savedIndexList){ selection.select(index, index); } tableView->selectionModel()->select(selection, QItemSelectionModel::Select);}
CustomSqlModel.hclass CustomSqlModel : public QSqlTableModel{public: CustomSqlModel(QTableView **parentTable, QObject *parent = 0); QTableView *tableView; QModelIndexList savedIndexList; void saveSelection(); bool setData (const QModelIndex & index, const QVariant & value, int role); void loadSelection();};
TableEditor.cppTableEditor::TableEditor(){ QTableView *view = new QTableView; model = new CustomSqlModel(&view, this);... connect(revertSelectionButton, SIGNAL(clicked()), this, SLOT(revertSelection())); connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(revertSelection()));...}void TableEditor::revertSelection(){ model->loadSelection();}
TableEditor.h...private slots: void revertSelection();private: QPushButton *revertSelectionButton;...
if (!(selected.size() == 0 && deselected.size() != 0)) return;