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[])
{
MainWindow w;
w.setWindowState(Qt::WindowMaximized);
w.show();
return a.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowState(Qt::WindowMaximized);
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp :
MainWindow
::MainWindow(QWidget *parent
){
setCentralWidget(myTableView);
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
databasemodel.h :
{
Q_OBJECT
public:
private:
};
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:
};
To copy to clipboard, switch view to plain text mode
databasemodel.cpp :
{
}
{
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks