Srv19 Srv19 - 3 months ago 26
C++ Question

QTableView view displays nothing

I am working with SQLite from QT, and can't seem to make code that is derived from one of examples to work properly. Namely, my QTableView displays nothing, even though its model is by no means empty.

Here is the code that creates QTableView:

QSqlTableModel model;


QTableView *view = new QTableView;
//view->setItemDelegate(new QSqlRelationalDelegate(view));

view->setWindowTitle(QObject::tr("Delivery Table"));


Here is the code that initializes the model:

static void initializeDeliveryModel(QSqlTableModel *model)
int t = model->columnCount(); //6
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 would no columns and rows be displayed?

Addendum. The QTableView object is created from inside of button click handler of my main form. When i copied the code from example as it was (and put the codew from main into handler), it ended up the same: no headers or rows were displayed.


Did you add your QTableView to corresponding form layout?