/*! Removes \a count rows starting at \a row. Since this model does not support hierarchical structures, \a parent must be an invalid model index. Emits the beforeDelete() signal before a row is deleted. When the edit strategy is OnManualSubmit signal emission is delayed until submitAll() is called. Returns true if all rows could be removed; otherwise returns false. Detailed error information can be retrieved using lastError(). \sa removeColumns(), insertRows() */ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) { Q_D(QSqlTableModel); if (parent.isValid() || row < 0 || count <= 0) return false; int i; switch (d->strategy) { case OnFieldChange: case OnRowChange: for (i = 0; i < count; ++i) { if (row + i == d->insertIndex) d->revertInsertedRow(); else if (!deleteRowFromTable(row + i)) return false; } select(); break; case OnManualSubmit: for (i = 0; i < count; ++i) { int idx = row + i; if (idx >= rowCount()) return false; if (d->cache.value(idx).op == QSqlTableModelPrivate::Insert) revertRow(idx); else { d->cache[idx].op = QSqlTableModelPrivate::Delete; emit headerDataChanged(Qt::Vertical, idx, idx); } } break; } return true; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); model = new QSqlRelationalTableModel(this); qmodel = new QSqlQueryModel(this); delegate = new QSqlRelationalDelegate(ui->tableView); faculties = new QStringList(); student = new Student(); visit = new Visit(); fillFaculties(); ui->facultyComboBox->setEditable(true); ui->groupComboBox->setEditable(true); //ui->tableView->setItemDelegateForColumn(QAbstractItemDelegate); //initializeModel(model); ui->tableView->setModel(model); ui->tableView->setItemDelegate(delegate); connect(ui->tablesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeDisplayedTable(int))); connect(ui->tablesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTableView())); connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteRowFromTable())); connect(ui->tableView, SIGNAL(clicked(QModelIndex)),this, SLOT(setEnabledDeleteButton(QModelIndex))); connect(ui->tablesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setDisabledDeleteButton(int))); connect(ui->facultyComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(fillGroups(QString))); displayStudentTable(model); }
/*! Submits all pending changes and returns \c true on success. Returns \c false on error, detailed error information can be obtained with lastError(). In OnManualSubmit, on success the model will be repopulated. Any views presenting it will lose their selections. Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted without losing data. \sa revertAll(), lastError() */ bool QSqlTableModel::submitAll() { Q_D(QSqlTableModel); bool success = true; foreach (int row, d->cache.keys()) { // be sure cache *still* contains the row since overridden selectRow() could have called select() QSqlTableModelPrivate::CacheMap::iterator it = d->cache.find(row); if (it == d->cache.end()) continue; QSqlTableModelPrivate::ModifiedRow &mrow = it.value(); if (mrow.submitted()) continue; switch (mrow.op()) { case QSqlTableModelPrivate::Insert: success = insertRowIntoTable(mrow.rec()); break; case QSqlTableModelPrivate::Update: success = updateRowInTable(row, mrow.rec()); break; case QSqlTableModelPrivate::Delete: success = deleteRowFromTable(row); break; case QSqlTableModelPrivate::None: Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation"); break; } if (success) { if (d->strategy != OnManualSubmit && mrow.op() == QSqlTableModelPrivate::Insert) { int c = mrow.rec().indexOf(d->autoColumn); if (c != -1 && !mrow.rec().isGenerated(c)) mrow.setValue(c, d->editQuery.lastInsertId()); } mrow.setSubmitted(); if (d->strategy != OnManualSubmit) success = selectRow(row); } if (!success) break; } if (success) { if (d->strategy == OnManualSubmit) success = select(); } return success; }
/*! Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with lastError(). On success the model will be repopulated. Any views presenting it will lose their selections. Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted again without losing data. \sa revertAll(), lastError() */ bool QSqlTableModel::submitAll() { Q_D(QSqlTableModel); switch (d->strategy) { case OnFieldChange: if (d->insertIndex == -1) return true; // else fall through case OnRowChange: if (d->editBuffer.isEmpty()) return true; if (d->insertIndex != -1) { if (!insertRowIntoTable(d->editBuffer)) return false; d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column()); } else { if (!updateRowInTable(d->editIndex, d->editBuffer)) return false; } d->clearEditBuffer(); d->editIndex = -1; d->insertIndex = -1; return select(); case OnManualSubmit: for (QSqlTableModelPrivate::CacheMap::ConstIterator it = d->cache.constBegin(); it != d->cache.constEnd(); ++it) { switch (it.value().op) { case QSqlTableModelPrivate::Insert: if (!insertRowIntoTable(it.value().rec)) return false; d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column()); break; case QSqlTableModelPrivate::Update: if (!updateRowInTable(it.key(), it.value().rec)) return false; break; case QSqlTableModelPrivate::Delete: if (!deleteRowFromTable(it.key())) return false; break; case QSqlTableModelPrivate::None: Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation"); break; } } d->clearCache(); return select(); } return false; }
/*! Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with lastError(). In OnManualSubmit, on success the model will be repopulated. Any views presenting it will lose their selections. Note: In OnManualSubmit mode, already submitted changes won't be cleared from the cache when submitAll() fails. This allows transactions to be rolled back and resubmitted without losing data. \sa revertAll(), lastError() */ bool QSqlTableModel::submitAll() { Q_D(QSqlTableModel); bool success = true; for (QSqlTableModelPrivate::CacheMap::Iterator it = d->cache.begin(); it != d->cache.end(); ++it) { if (it.value().submitted()) continue; switch (it.value().op()) { case QSqlTableModelPrivate::Insert: success = insertRowIntoTable(it.value().rec()); break; case QSqlTableModelPrivate::Update: success = updateRowInTable(it.key(), it.value().rec()); break; case QSqlTableModelPrivate::Delete: success = deleteRowFromTable(it.key()); break; case QSqlTableModelPrivate::None: Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation"); break; } if (success) { it.value().setSubmitted(); if (d->strategy != OnManualSubmit) success = selectRow(it.key()); } if (!success) break; } if (success) { if (d->strategy == OnManualSubmit) success = select(); } return success; }
/*! Removes \a count rows starting at \a row. Since this model does not support hierarchical structures, \a parent must be an invalid model index. Emits the beforeDelete() signal before a row is deleted. When the edit strategy is OnManualSubmit signal emission is delayed until submitAll() is called. Returns true if all rows could be removed; otherwise returns false. Detailed error information can be retrieved using lastError(). \sa removeColumns(), insertRows() */ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) { Q_D(QSqlTableModel); if (parent.isValid() || row < 0 || count <= 0) return false; int i; switch (d->strategy) { case OnFieldChange: case OnRowChange: for (i = 0; i < count; ++i) { if (row + i == d->insertIndex) d->revertInsertedRow(); else if (!deleteRowFromTable(row + i)) return false; } select(); break; case OnManualSubmit: for (i = 0; i < count; ++i) { int idx = row + i; if (idx >= rowCount()) return false; if (d->cache.value(idx).op == QSqlTableModelPrivate::Insert) { revertRow(idx); // Reverting a row means all the other cache entries have been adjusted downwards // so fake this by adjusting row --row; } else { d->cache[idx].op = QSqlTableModelPrivate::Delete; d->cache[idx].primaryValues = d->primaryValues(indexInQuery(createIndex(idx, 0)).row()); emit headerDataChanged(Qt::Vertical, idx, idx); } } break; } return true; }