Reputation: 113
I am trying to prototype a window with a QTableView
. There isn't yet any database behind it, but there will be at some point. I can't get the tableview to show anything. I get a rectangle with white space. I've looked at the examples online and seem to be doing everything right, as far as I can tell.
I set up a QTableView
object with the GUI builder and then added the following code immediately after the call to ui.setupUi(this);
// set up prototype table model to hold dummy data
QSqlTableModel * model = new QSqlTableModel(this);
model->setTable("Errors");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
// set header names
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Error Number"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Message"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Details"));
// insert a dummy record
{
QSqlRecord record;
QSqlField field1("Errno", QVariant::Int);
field1.setValue(1);
record.insert(0, field1);
QSqlField field2("Msg", QVariant::String);
field2.setValue(QString("Unable to perform snapshot"));
record.insert(1, field2);
QSqlField field3("Details", QVariant::String);
field3.setValue(QString("Unable to perform snapshot. Please try again"));
record.insert(2, field3);
model->insertRecord(0, record);
}
// insert a dummy record
{
QSqlRecord record;
QSqlField field1("Errno", QVariant::Int);
field1.setValue(1);
record.insert(0, field1);
QSqlField field2("Msg", QVariant::String);
field2.setValue(QString("Unable to perform snapshot"));
record.insert(1, field2);
QSqlField field3("Details", QVariant::String);
field3.setValue(QString("Unable to perform snapshot. Please try again"));
record.insert(2, field3);
model->insertRecord(1, record);
}
ui.errMsgsTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui.errMsgsTableView->setModel(model);
ui.errMsgsTableView->show();
Anyone have any suggestions? What am I missing?
Upvotes: 1
Views: 587
Reputation: 98425
You cannot use a QSqlTableModel
without a database. As a quick workaround, you could connect it to a temporary in-memory sqlite table that literally can be created in two statements. For prototyping you should probably use a QStandardItemModel
, though. Here's a complete example:
// https://github.com/KubaO/stackoverflown/tree/master/questions/dummymodel-37577922
#include <QtWidgets>
template <typename T> QStandardItem * newItem(const T val) {
auto item = new QStandardItem;
item->setData(val, Qt::DisplayRole);
return item;
}
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QStandardItemModel model;
model.setColumnCount(3);
model.setHorizontalHeaderLabels(QStringList{"Error Number", "Message", "Details"});
auto newRow = []{ return QList<QStandardItem*>{
newItem(1),
newItem("Unable to perform snapshot"),
newItem("Unable to perform snapshot. Please try again")};
};
model.appendRow(newRow());
model.appendRow(newRow());
QTableView view;
view.setModel(&model);
view.show();
return app.exec();
}
Upvotes: 1