QTableView view displays nothing
I am working with SQLite from Qt
and I can't seem to make code that is derived from one of examples to work properly. My QTableView displays nothing, even though its model
is by 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"),
开发者_如何学JAVA 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 code from main into handler), it ended up the same: no headers or rows were displayed.
Did you add your QTableView
to corresponding form layout?
It seems that you did not initialize the model at the same time. If you initialize the model after associating with the view, it is your responsibility to call QAbstractItemView::update()
. Passing the default QModelIndex
may do the trick.
My solution to this problem, given that your QTableView widget is created properly, the model data is set, and it shows up as an empty box in your GUI. You need to call:
emit layoutChanged();
after you set your data to the model. The table should populate after this.
精彩评论