This thread has really helped me out, but I am still somewhat confused about what appears to be the foundation of the problem (I am fairly new to C++):
How do you know if you should be creating a pointer or not? How do I know when to use *model or model?
The documentation from Trolltech (http://doc.trolltech.com/4.3/qsqlquerymodel.html) actually makes it even worse for a beginner because they seem to use the two interchangeably - but when I do so, I get Segmentation faults if I can compile at all:
model->setQuery("SELECT name, salary FROM employee");
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
view->setModel(model);
view->show();
We set the model's query, then we set up the labels displayed in the view header.
QSqlQueryModel can also be used to access a database programmatically, without binding it to a view:
QSqlQueryModel model;
model.setQuery("SELECT * FROM employee");
int salary = model.record(4).value("salary").toInt();
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT name, salary FROM employee");
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
QTableView *view = new QTableView;
view->setModel(model);
view->show();
We set the model's query, then we set up the labels displayed in the view header.
QSqlQueryModel can also be used to access a database programmatically, without binding it to a view:
QSqlQueryModel model;
model.setQuery("SELECT * FROM employee");
int salary = model.record(4).value("salary").toInt();
To copy to clipboard, switch view to plain text mode
Bookmarks