Unsolved TableView doesnt' change cell text?
-
@devhobby said in TableView doesnt' change cell text?:
does nothing.
just as i said. You don't have access to the model internal data, so you wont be able to modify it, unless you reload the complete model from the database (which also means you will loose the current selection).
But you still refuse to tell why you don't use a QSqlTableModel. So i can't help you anymore.
-
@raven-worx said in TableView doesnt' change cell text?:
@devhobby said in TableView doesnt' change cell text?:
does nothing.
just as i said. You don't have access to the model internal data, so you wont be able to modify it, unless you reload the complete model from the database (which also means you will loose the current selection).
Is it really the only way?
From Qt Documentation
The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel
It says to reimplement setData() but I did... i'm just missing the part where the data is changed inside the Table
Also, it says ANOTHER option is to use QSqlTableModel
But you still refuse to tell why you don't use a QSqlTableModel. So i can't help you anymore.
I prefer QSqlQueryModel because I use the method setQuery() which is protected inside QSqlTableModel.
The latter only wants a table
-
@devhobby
see yourself. The data() method just returns the data directly from the QSqlQuery.a workaround could be
- cache the modified data (passed to setData()) - but still update the value to the DB
- override data() and if there is cached data return it, otherwise simply return the base-class implementation
- on a model reset clear the cached data again
-
@raven-worx said in TableView doesnt' change cell text?:
@devhobby
see yourself. The data() method just returns the data directly from the QSqlQuery.a workaround could be
- cache the modified data (passed to setData()) - but still update the value to the DB
- override data() and if there is cached data return it, otherwise simply return the base-class implementation
- on a model reset clear the cached data again
One thing I forgot to tell you.
The data() function grabs the item and the role.
If the role is QtBackground color the cell Blue if it has been edited
QVariant MyModel::data(const QModelIndex& item, int role) const { if (!item.isValid()) return QVariant(); if(role == Qt::BackgroundRole) { if(MainWindow::cellsEdited.contains(item)) return QColor(66, 197, 244, 150); } return QSqlQueryModel::data(item, role); }
If not, return the base class data()
Is there something I need to do here?
-
@devhobby said in TableView doesnt' change cell text?:
MainWindow::cellsEdited.contains(item)
What's this?
-
@VRonin said in TableView doesnt' change cell text?:
@devhobby said in TableView doesnt' change cell text?:
MainWindow::cellsEdited.contains(item)
What's this?
The slot onTableClicked() gets called when a cell of the Table View gets edited.
When that happens, I insert in the cellsEdited array the cell (which is a QModelIndex)
In data() I check if the cell is one of the edited ones. If yes, color that cell with Blue
-
Any idea?
I saw in the QSqlTableModel source code that the method setData() calls QSqlTableModelPrivate::setGeneratedValue to set the field in the table view.
QSqlQueryModel has no such method. But then, how the hell do I change the field text?
-
@devhobby said in TableView doesnt' change cell text?:
QSqlQueryModel has no such method. But then, how the hell do I change the field text?
you need to return your cached data in the data() method for the
Qt::DisplayRole
Also you shouldn't useQModelIndex
for persiting purposes. UseQPersistentModelIndex
for such cases. -
@raven-worx said in TableView doesnt' change cell text?:
@devhobby said in TableView doesnt' change cell text?:
QSqlQueryModel has no such method. But then, how the hell do I change the field text?
you need to return your cached data in the data() method for the
Qt::DisplayRole
I tried and worked! But tell me if I used the right approach
I used a QMap<QModelIndex, QVariant> to assign each index its new data
When data() is called, if the role is DisplayRole and there is cached data for the index, return its variant
Also you shouldn't use
QModelIndex
for persiting purposes. UseQPersistentModelIndex
for such cases.Could you explain this point a little more? Also, the overriding functions require a QModelIndex
-
@devhobby said in TableView doesnt' change cell text?:
Could you explain this point a little more? Also, the overriding functions require a QModelIndex
Just replace
QMap<QModelIndex, QVariant>
withQMap<QPersistentModelIndex, QVariant>
.
You can use a QPersistentModelIndex everywhere where a QModelIndex is expcted - no conversion needed.