问题
After making connection with the database , I have written the following code, but after execution view's inner cells are empty. WHy?
enum {
name = 0,
clas = 1,
launched = 2
};
QSqlTableModel* model = new QSqlTableModel;
model->setTable("ships");
model->setSort(name, Qt::AscendingOrder);
model->setHeaderData(name, Qt::Horizontal, "Name");
model->setHeaderData(clas, Qt::Horizontal, "class");
model->setHeaderData(launched, Qt::Horizontal, "launched");
model->select();
QTableView * view = new QTableView;
view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->resizeColumnsToContents();
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
QHeaderView *header = view->horizontalHeader();
header->setStretchLastSection(true);
view->show();
来源:https://stackoverflow.com/questions/41078500/qts-model-view-architecture