QTableWidget - inserting data
Hello!
I want to show data from my database in QTableWidget. I've wrote somehing like that:
Code:
bool MainWindow::readFromDatabase()
{
[...]
ok = bdb.open();
if (ok)
{
QSqlQuery query
("SELECT * FROM mytable", bdb
);
while (query.next()) {
row = ui->qtable->rowCount();
ui->qtable->insertRow(row);
ui->qtable->setItem(row, 0, item1);
ui->qtable->setItem(row, 1, item2);
[...]
}
query.clear();
} else {
//out << "database open error!" << endl;
}
bdb.close();
return ok;
}
Is it the only way of inserting items into database? Is it necessary to create all these items? Maybe there is way to do it simpler (set position and text which should be there)?
thanks in advance
best regards
Tomasz
Re: QTableWidget - inserting data
Use the model view framework. See QSqlTableModel or QSqlQueryModel as well as the introduction of the model view framework in the docs.