Pj Toopmuch
Pj Toopmuch

Reputation: 147

QSqlTableModel::removeRow() does not work setting QSqlTableModel::OnManualSubmit

I'm removing a row from a DB table that works using EditStrategy OnRowChange. If I set OnManualSubmit it doesn't work. I do submitAll() from QML side and in the C++ function but the row doesn't get deleted.

I inherit QSqlTableModel as MDataBaseTableModel because I need to show images from the SQLite DB in QML.

So I dont get why removeRow() does work on EditStragy::OnRowChange, but not on EditStragy::onManualSubmit?

Here is the code:

MDatabaseTableModel.cpp

MDataBaseTableModel::MDataBaseTableModel(QObject *parent, QSqlDatabase db,QString table):
QSqlTableModel(parent, db)
{
setEditStrategy(QSqlTableModel::OnManualSubmit);
setTable(table);
select();
}

QVariant MDataBaseTableModel::data(const QModelIndex &index, int role) const
{
QVariant value;

if (index.isValid()) {
    if (role < Qt::UserRole) {
        value = QSqlTableModel::data(index, role);
    } else {
        int columnIdx = role - Qt::UserRole - 1;
        QModelIndex modelIndex = this->index(index.row(), columnIdx);
        value = QSqlTableModel::data(modelIndex, Qt::DisplayRole);
        if(roleNames().value(role) == "sqlImageData")
            return QImage::fromData(value.toByteArray());
    }
}
return value;
}

QHash<int, QByteArray> MDataBaseTableModel::roleNames() const
{

QHash<int, QByteArray> roles;
for (int i = 0; i < record().count(); i ++) {
    roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
}
return roles;
}

bool MDataBaseTableModel::setData(const QModelIndex &item, const QVariant &value, int role)
{
if (item.isValid() && role == Qt::EditRole) {
    QSqlTableModel::setData(item, value, role);
    emit dataChanged(item, item);
    return true;
}
return false;
}

void MDataBaseTableModel::removeRow(int row){

qDebug()<< removeRows(row, 1, QModelIndex());
select();
submitAll();
}

MDataBaseTableModel.h

class MDataBaseTableModel:public QSqlTableModel
{
Q_OBJECT
public:
explicit MDataBaseTableModel(QObject * parent =0,QSqlDatabase db=QSqlDatabase(),QString, table="");
void connectDb();
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
bool setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE void removeRow(int row);
};

main.cpp

QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName("products.sqlite");
db.open()
MDataBaseTableModel* categoryModel = new MDataBaseTableModel(0,db,"categoryTable");
engine.rootContext()->setContextProperty("categorySQL", categoryModel);

main.qml

ListView {
id: listView

implicitWidth: window.width /5
implicitHeight: window.height/4

model : categorySQL
delegate: ItemDelegate{

width: parent.width

MouseArea {
  anchors.fill: parent
  onClicked: {
    listView.currentIndex = index
}
}
Text{
  text : sqlName
  }                    
}
}
Button {
  text: "-"
  onClicked: {
    categorySQL.removeRow(listView.currentIndex)
    categorySQL.submitAll()
  }
}

Upvotes: 0

Views: 396

Answers (1)

Pj Toopmuch
Pj Toopmuch

Reputation: 147

The probem was select(); before submitAll();

void MDataBaseTableModel::removeRow(int row){
qDebug()<< removeRows(row, 1, QModelIndex());
select();
submitAll();
}

Upvotes: 1

Related Questions