PDA

View Full Version : Editable SqlQueryModel



Miss Engineer
14th May 2014, 11:32
Hello,
I have a TableView that uses an SqlQueryModel. I want some of the cells in the table to be editable but the edits should not affect the original database. I subclassed SqlQueryModel with ItemFlags that allow it to be editiable. Because I do not want the changes to affect the database, I can't re implement the setData function, and so any edits get ignored as soon as I hit enter. Any ideas on how I can get the data entered in the view to stay there without affecting the model? Also, how can I tell the subclassed SqlQueryModel that I only want the cells in the second column that do not contain data to be editable?
The following is my subclass:


#include "editablesqlmodel.h"
class EditableSQLModel;

EditableSQLModel::EditableSQLModel(QObject *parent):
QSqlQueryModel(parent)
{

}
Qt::ItemFlags EditableSQLModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 1) flags |= Qt::ItemIsEditable;
return flags;
}

anda_skoa
14th May 2014, 12:28
You will have to reimplement the setData() function and store the edited data in your model subclass.
In data() you then need to check if you have local data for the respective cell and return it instead.

From you description I take that you have only one such column, so a simple mapping from inf (row) to QVariant (data) should do the trick for the local storage.

Cheers,
_

Miss Engineer
14th May 2014, 15:16
You will have to reimplement the setData() function and store the edited data in your model subclass.
In data() you then need to check if you have local data for the respective cell and return it instead.

From you description I take that you have only one such column, so a simple mapping from inf (row) to QVariant (data) should do the trick for the local storage.

Cheers,
_
Thanks for the tip.. However, being a beginner, I can't wrap my head around it..
tried this and I know something is missing as setData is not calling Data()


QByteRef EditableSQLModel::data(const QModelIndex &index, int role)
{
QVariant value = QSqlQueryModel::data(index, role);
if (value!="")
return value.toByteArray();
else
return ArgsLength;

}
bool EditableSQLModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
{
if (index.column() !=1)
return false;
ArgsLength= value;



return 1;
}

anda_skoa
14th May 2014, 16:27
You would have something like this



class MyModel : public QSqlQueryModel
{
private:
QHash<int, QVariant> m_localData;
};


In setData() you would store depending on row



if (index.column() == 1) {
m_localData.insert(index.row(), value);
}

in data() you would check if you have local data


if (index.column() == 1 && m_localData.contains(index.row()) {
return m_localData.value(index.row())
}
// else get value from base class


Cheers,
_

Miss Engineer
15th May 2014, 01:59
You would have something like this



class MyModel : public QSqlQueryModel
{
private:
QHash<int, QVariant> m_localData;
};


In setData() you would store depending on row



if (index.column() == 1) {
m_localData.insert(index.row(), value);
}

in data() you would check if you have local data


if (index.column() == 1 && m_localData.contains(index.row()) {
return m_localData.value(index.row())
}
// else get value from base class


Cheers,
_

Thanks a lot.. Now it makes sense :)