toma
7th July 2014, 23:17
Hi everybody.
I'm trying to re-implement the setData() function of the QSqlRelationalTableModel class but, as you already guessed it, I'm unsuccessful...
Already developed an app which uses delegates, overloads functions flags(), data(), rowCount(), columCount()... but as I can't figure out what I'm doing wrong in the setData() function, I made another app with the less code as I could in an attempt to debug it and to share it with you.
So... here it is (the buggy part is in databasemodel.cpp):
main.cpp :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowState(Qt::WindowMaximized);
w.show();
return a.exec();
}
mainwindow.cpp :
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
myTableView = new QTableView(this);
setCentralWidget(myTableView);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("../mydb.db");
db.open();
modelConducteurs = new DataBaseModel(this, db);
modelConducteurs->setTable("conducteurs");
modelConducteurs->select();
myTableView->setModel(modelConducteurs);
}
MainWindow::~MainWindow()
{
delete myTableView;
delete modelConducteurs;
}
databasemodel.h :
class DataBaseModel : public QSqlRelationalTableModel
{
Q_OBJECT
public:
DataBaseModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase());
bool setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole);
private:
};
databasemodel.cpp :
EditDataBaseModel::EditDataBaseModel(QObject *parent, QSqlDatabase db)
: QSqlRelationalTableModel(parent, db)
{
}
bool EditDataBaseModel::setData(const QModelIndex &item, const QVariant &value, int role)
{
this->query().seek(item.row());
switch (role) {
case Qt::EditRole:
qDebug() << value;
this->query().record().setValue(item.column(), value);
qDebug() << this->query().lastError();
qDebug() << this->query().record().value(item.column());
emit dataChanged(item, item);
return true;
break;
default:
break;
}
return false;
}
The problem is the following : the setValue() function seems to be ineffective :
- The value of value is correct (displayed thanks to qDebug).
- qDebug() << this->query().lastError(); returns QSqlError(-1, "", "") (no error).
- qDebug() << this->query().record().value(item.column()); gives me the unchanged content of the record. As if I had never used setValue() two lines before.
Did I misunderstood the way I'm supposed to update the model data ?
Is there another function than setValue that I should be using (using setRecord in setData only leads me to a crashing app) ?
Thanks in advance for you answers.
See you.
Thomas.
I'm trying to re-implement the setData() function of the QSqlRelationalTableModel class but, as you already guessed it, I'm unsuccessful...
Already developed an app which uses delegates, overloads functions flags(), data(), rowCount(), columCount()... but as I can't figure out what I'm doing wrong in the setData() function, I made another app with the less code as I could in an attempt to debug it and to share it with you.
So... here it is (the buggy part is in databasemodel.cpp):
main.cpp :
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowState(Qt::WindowMaximized);
w.show();
return a.exec();
}
mainwindow.cpp :
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
myTableView = new QTableView(this);
setCentralWidget(myTableView);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("../mydb.db");
db.open();
modelConducteurs = new DataBaseModel(this, db);
modelConducteurs->setTable("conducteurs");
modelConducteurs->select();
myTableView->setModel(modelConducteurs);
}
MainWindow::~MainWindow()
{
delete myTableView;
delete modelConducteurs;
}
databasemodel.h :
class DataBaseModel : public QSqlRelationalTableModel
{
Q_OBJECT
public:
DataBaseModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase());
bool setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole);
private:
};
databasemodel.cpp :
EditDataBaseModel::EditDataBaseModel(QObject *parent, QSqlDatabase db)
: QSqlRelationalTableModel(parent, db)
{
}
bool EditDataBaseModel::setData(const QModelIndex &item, const QVariant &value, int role)
{
this->query().seek(item.row());
switch (role) {
case Qt::EditRole:
qDebug() << value;
this->query().record().setValue(item.column(), value);
qDebug() << this->query().lastError();
qDebug() << this->query().record().value(item.column());
emit dataChanged(item, item);
return true;
break;
default:
break;
}
return false;
}
The problem is the following : the setValue() function seems to be ineffective :
- The value of value is correct (displayed thanks to qDebug).
- qDebug() << this->query().lastError(); returns QSqlError(-1, "", "") (no error).
- qDebug() << this->query().record().value(item.column()); gives me the unchanged content of the record. As if I had never used setValue() two lines before.
Did I misunderstood the way I'm supposed to update the model data ?
Is there another function than setValue that I should be using (using setRecord in setData only leads me to a crashing app) ?
Thanks in advance for you answers.
See you.
Thomas.