Unsolved sourceModel()->removeRows returns false although it removes the rows
-
QSortFilterProxyModel can take care of it, you don't have to do it manually. did you try my code?
-
Yes I did but now (without the
beginRemoveRows()
andendRemoveRows()
) the view is not updated anymore and the changes are only visible after restarting the application. -
This is strange as the source for QSortFilterProxyModel does not call
beginRemoveRows()
andendRemoveRows()
let me test it...EDIT:
could you attach a minimal example of the sqlite database? -
create table measurements(date integer, voltage real, current real, power real, energy real); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.0, 1.1, 2.2, 3.3); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.1, 1.2, 2.3, 3.4); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.2, 1.3, 2.4, 3.5); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.3, 1.4, 2.5, 3.6); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.4, 1.5, 2.6, 3.7); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.5, 1.6, 2.7, 3.8); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 0.6, 1.7, 2.8, 3.9); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.0, 2.1, 3.2, 4.3); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.1, 2.2, 3.3, 4.4); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.2, 2.3, 3.4, 4.5); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.3, 2.4, 3.5, 4.6); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.4, 2.5, 3.6, 4.7); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.5, 2.6, 3.7, 4.8); insert into measurements(date,voltage,current,power,energy) values(strftime('%Y-%m-%d %H:%M:%S','now'), 1.6, 2.7, 3.8, 4.9);
Just save this as
testdata.sql
and runsqlite3 qnergenie.sqlite3 <testdata.sql
... And of course change the path inmain.cpp
Thanks.
-
Looks like it's a problem with the
submit()
of the base model.
To make it work addvirtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
toSqlTableModel
with this body:bool SqlTableModel::removeRows(int row, int count, const QModelIndex &parent) { bool success = false; const EditStrategy oldStartegy=editStrategy(); setEditStrategy(QSqlTableModel::OnManualSubmit); if (QSqlTableModel::removeRows(row,count,parent)) { if (submitAll()) { success=true; qDebug() << "Yay!"; } else { qDebug() << lastError(); } } setEditStrategy(oldStartegy); return success; }
and the proxy is simply:
void ProxyModel::deleteRow(int row) { if (removeRow(row)) { qDebug() << "Yay!"; } else { qDebug() << "Nay!"; } }
If someone has any insight why submit fails even if the rows removal is successful I'm curious now
-
On a separate note
ProxyModel::setEnergyFilter
should become:void ProxyModel::setEnergyFilter(double filterValue) { qDebug() << "setEnergyFilter"; if(m_energyFilter==filterValue) return; m_energyFilter = filterValue; invalidateFilter(); emit energyFilterChanged(); }
-
I applied your changes to the models and it .. well, still works, just with a
Yay!
now. I am puzzled. :)In the beginning it was even weirder. I could reproduce a situation where it also returned
Yay!
when it failed. So it was just upside-down. ... Not sure what I did at that point, though.Regarding
ProxyModel::setEnergyFilter
:
Thanks for the hints. The early exit is a no-brainer. I am just in the "make it work" phase before getting to applying the best practices I picked-up so far.
But what about the signal? It works without it. Do you know why?Thanks so far for digging into this. :)
-
The filter is just for QML.
If you declareQ_PROPERTY(double energyFilter READ energyFilter WRITE setEnergyFilter NOTIFY energyFilterChanged)
QML wil rely on the energyFilterChanged signal to know when the filter changes but the signal has to be emitted manually by your QObject. see http://doc.qt.io/qt-5/properties.html#a-simple-example -
Ok, I see how it is meant to be and will follow the lead. :)
Still, it works just fine without emitting the signal. Courious.So two questions remain for me:
- why submit fails even if the rows removal is successful
- why the filter works fine without emitting the signal
Regards
-
As for why the filter works fine without emitting the signal it's that the view is updated just fine without sending the signal but the property itself is not updated. So if I just print out the property somewhere in the QML GUI, its value remains the initial value.