QSqlQuery query; if(!query.exec("SELECT * FROM test_scheme.computers;")){ qDebug()<<"Bad exec!"; return; } qDebug()<<"Success!"; model = new QSqlTableModel(this, db); for(int i = 0; query.next(); i++){ model->insertRecord(i, query.record()); } table->setModel(model);
while(query.next()){ model->insertRecord(i, query.record()); }
int i = 0;while(query.next()){ model->insertRecord(i, query.record()); i++;}
void QSqlTableModel::setTable ( const QString & tableName ) [virtual]
void QSqlQueryModel::setQuery ( const QSqlQuery & query )
C++ (Qt) QSqlTableModel *model = new QSqlTableModel(parentObject, database); model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); model->setHeaderData(0, Qt::Horizontal, tr("Name")); model->setHeaderData(1, Qt::Horizontal, tr("Salary")); QTableView *view = new QTableView; view->setModel(model); view->hideColumn(0); // don't show the ID view->show();
select a.id, t.name, c.name, c.ip, a.is_online from test_scheme.arms a, test_scheme.arm_types t, test_scheme.computers c where a.comp_id = c.id and a.type_id = t.id;
query = new QSqlQuery("select a.id, t.name, c.name, c.ip, a.is_online from test_scheme.arms a, test_scheme.arm_types t, test_scheme.computers c where a.comp_id = c.id and a.type_id = t.id;",db); model->setQuery(*query); table->setModel(model);
query = new QSqlQuery("select a.id, t.name, c.name, c.ip, a.is_online from kedr.arms a, kedr.arms_types t, kedr.computers c where a.computers_id = c.id and a.type_id = t.id;",db); model->setQuery(*query); model->setData(model->index(2,2), Qt::BackgroundRole, Qt::red); QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this); sqlproxy->setSourceModel(model); sqlproxy->setData(sqlproxy->index(2,2), Qt::BackgroundRole, Qt::red); sqlproxy->sort(sorted_col, Qt::AscendingOrder); table->verticalHeader()->hide(); table->setModel(sqlproxy); table->model()->setData(table->model()->index(2,2), Qt::BackgroundRole, Qt::red); table->repaint();