c++ - QTableView doesn't show anything -
i trying prototype window qtableview
. there isn't yet database behind it, there @ point. can't tableview show anything. rectangle white space. i've looked @ examples online , seem doing right, far can tell.
i set qtableview
object gui builder , added following code after call ui.setupui(this);
// set prototype table model 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 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 perform snapshot")); record.insert(1, field2); qsqlfield field3("details", qvariant::string); field3.setvalue(qstring("unable perform snapshot. please try again")); record.insert(2, field3); model->insertrecord(0, record); } // insert 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 perform snapshot")); record.insert(1, field2); qsqlfield field3("details", qvariant::string); field3.setvalue(qstring("unable 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 suggestions? missing?
you cannot use qsqltablemodel
without database. quick workaround, connect temporary in-memory sqlite table literally can created in 2 statements. prototyping should use qstandarditemmodel
, though. here's 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 perform snapshot"), newitem("unable perform snapshot. please try again")}; }; model.appendrow(newrow()); model.appendrow(newrow()); qtableview view; view.setmodel(&model); view.show(); return app.exec(); }