diff --git a/src/ExtendedTableWidget.cpp b/src/ExtendedTableWidget.cpp index 171d12da..2f67b481 100644 --- a/src/ExtendedTableWidget.cpp +++ b/src/ExtendedTableWidget.cpp @@ -362,8 +362,7 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : }); connect(nullAction, &QAction::triggered, [&]() { - for(const QModelIndex& index : selectedIndexes()) - model()->setData(index, QVariant()); + setToNull(selectedIndexes()); }); connect(copyAction, &QAction::triggered, [&]() { copy(false, false); @@ -810,8 +809,7 @@ void ExtendedTableWidget::keyPressEvent(QKeyEvent* event) if(event->modifiers().testFlag(Qt::AltModifier)) { // When pressing Alt+Delete set the value to NULL - for(const QModelIndex& index : selectedIndexes()) - model()->setData(index, QVariant()); + setToNull(selectedIndexes()); } else { // When pressing Delete only set the value to empty string for(const QModelIndex& index : selectedIndexes()) @@ -1043,3 +1041,24 @@ void ExtendedTableWidget::currentChanged(const QModelIndex ¤t, const QMode QTableView::currentChanged(current, previous); emit currentIndexChanged(current, previous); } + +void ExtendedTableWidget::setToNull(const QModelIndexList& indices) +{ + SqliteTableModel* m = qobject_cast(const_cast(model())); + sqlb::TablePtr currentTable = m->db().getObjectByName(m->currentTableName()); + + // Check if some column in the selection has a NOT NULL constraint, before trying to update the cells. + if(currentTable) + for(const QModelIndex& index : indices) { + const sqlb::Field& field = currentTable->fields.at(static_cast(index.column())-1); + if(field.notnull()) { + QMessageBox::warning(nullptr, qApp->applicationName(), + tr("Cannot set selection to NULL. Column %1 has a NOT NULL constraint."). + arg(QString::fromStdString(field.name()))); + return; + } + } + + for(const QModelIndex& index : indices) + model()->setData(index, QVariant()); +} diff --git a/src/ExtendedTableWidget.h b/src/ExtendedTableWidget.h index 53a2cfff..b7c95a67 100644 --- a/src/ExtendedTableWidget.h +++ b/src/ExtendedTableWidget.h @@ -84,6 +84,8 @@ private: void useAsFilter(const QString& filterOperator, bool binary = false, const QString& operatorSuffix = QString()); void duplicateUpperCell(); + void setToNull(const QModelIndexList& indices); + static std::vector> m_buffer; static QString m_generatorStamp;