Nothing displayed in QTableView

I am working with SQLite from Qt , and I cannot make the code obtained from one of the examples to work correctly. My QTableView does not display anything, although its model no means empty:

 QSqlTableModel model; initializeDeliveryModel(&model); QTableView *view = new QTableView; view->setModel(&model); //view->setItemDelegate(new QSqlRelationalDelegate(view)); view->setWindowTitle(QObject::tr("Delivery Table")); view->show(); 

Model initialization code:

 static void initializeDeliveryModel(QSqlTableModel *model) { model->setTable("DELIVERY"); int t = model->columnCount(); //6 model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); model->setHeaderData(1, Qt::Horizontal, QObject::tr("Number")); model->setHeaderData(2, Qt::Horizontal, QObject::tr("Complection")); model->setHeaderData(3, Qt::Horizontal, QObject::tr("Has Arrived")); model->setHeaderData(4, Qt::Horizontal, QObject::tr("Dealer")); model->setHeaderData(5, Qt::Horizontal, QObject::tr("Price")); if (!model->select()) { QSqlError err = model->lastError(); QMessageBox::information(0, qApp->tr("Failed to select data from table"), err.text(), QMessageBox::Ok); } t = model->rowCount(); // 18 on the last debug } 

What am I missing here? Why are columns and rows not displayed?

Addition. The QTableView object is created from within the button handler of my main form. When I copied the code from the example as it was (and put the code from main into the handler), it turned out to be the same: no headers and lines were displayed.

+4
source share
2 answers

Have you added a QTableView to the appropriate form layout?

+2
source

It seems that you did not initialize the model at the same time. If you initialize the model after matching with the view, you are responsible for calling QAbstractItemView::update() . Passing a standard QModelIndex can do the trick.

+1
source

All Articles