Hey @all,
i've a problem with a subclass of a QSqlQueryModel.
After I insert data into the database (SQLITE) and refresh the model, the view is not refreshed.
Here the code of the subclass:
// Innitialize the model
refreshModel();
}
void MyModel::addRecord() {
bool ok = query.exec("INSERT INTO ......");
if(!ok) {
err = query.lastError();
qDebug() << err.text();
return;
}
refreshModel();
}
if(index.isValid() && role == Qt::BackgroundRole) {
// Alternating row color
if(index.row() % 2 == 0)
}
}
void MyModel::refreshModel() {
clear();
// Set the Header fields
// ...
}
MyModel::MyModel(QObject *parent) : QSqlQueryModel(parent) {
// Innitialize the model
refreshModel();
}
void MyModel::addRecord() {
QSqlQuery query;
bool ok = query.exec("INSERT INTO ......");
if(!ok) {
QSqlError err;
err = query.lastError();
qDebug() << err.text();
return;
}
refreshModel();
}
QVariant MyModel::data(const QModelIndex &index, int role) const {
if(index.isValid() && role == Qt::BackgroundRole) {
QPalette palette;
// Alternating row color
if(index.row() % 2 == 0)
return palette.color(QPalette::Active, QPalette::Base);
return palette.color(QPalette::Active, QPalette::AlternateBase);
}
return QSqlQueryModel::data(index, role);
}
void MyModel::refreshModel() {
clear();
QSqlQueryModel::setQuery("SELECT field1, field2 field3, ....");
// Set the Header fields
// ...
}
To copy to clipboard, switch view to plain text mode
m_ui->tableView->setModel(myModel);
m_ui->tableView->hideColumn(0);
m_ui->tableView->verticalHeader()->hide();
m_ui->tableView->horizontalHeader()->hide();
m_ui->tableView->setModel(myModel);
m_ui->tableView->hideColumn(0);
m_ui->tableView->verticalHeader()->hide();
m_ui->tableView->horizontalHeader()->hide();
To copy to clipboard, switch view to plain text mode
The program never went into "if(!ok) {". In the debugger the refreshModel() is called, but the TableView isn't refreshed.
Can anybody help?
Thanks in advance
Best Regards
NoRulez
Bookmarks