Hello!
I want to show data from my database in QTableWidget. I've wrote somehing like that:
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;
}
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);
QTableWidgetItem *item1 = new QTableWidgetItem(query.value(0).toString());
ui->qtable->setItem(row, 0, item1);
QTableWidgetItem *item2 = new QTableWidgetItem(query.value(1).toString());
ui->qtable->setItem(row, 1, item2);
[...]
}
query.clear();
} else {
//out << "database open error!" << endl;
}
bdb.close();
return ok;
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks