PDA

View Full Version : QTableView is not refreshed



NoRulez
13th October 2009, 21:27
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:


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
// ...
}




m_ui->tableView->setModel(myModel);
m_ui->tableView->hideColumn(0);
m_ui->tableView->verticalHeader()->hide();
m_ui->tableView->horizontalHeader()->hide();


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

NoRulez
13th October 2009, 22:23
In the main program, I've used references instead of pointers.

Thanks anyway

Best Regards
NoRulez

How can i mark a thread as solved?