PDA

View Full Version : QTableView displays column headers, but no data?



artephius
9th March 2015, 22:09
Edit: I think this is solved, appears to be a qt or qt designer bug -- see bottom of post...



I can't figure out why my QTableView won't display any data. I've searched all other questions about this problem and it seems the problem is usually someone trying to create the model on the stack and letting it go out of scope...

I am creating the model on the heap so this is not the problem, yet I still get no data in the View. The column headers from my sql table ARE shown correctly in the view. What could be wrong with this code?


// db is my database wrapper and database() returns a reference to the database
QSqlTableModel *tradeHistoryModel = new QSqlTableModel(this, db->database());

// table_tradeHistory is my QTableView created elsewhere
table_tradeHistory->setModel(tradeHistoryModel);

tradeHistoryModel->setTable("mytrades");
tradeHistoryModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

if (!tradeHistoryModel->select())
{
// err is just a handy macro for displaying fancy error output
// the call to select does NOT return false so this never gets called
err("model select failed!");
}

// debug is another macro, the output shows 200 rows (which is correct)
debug(QString("Got %1 rows..").arg(tradeHistoryModel->rowCount()));

So there is no error shown because select() returns true, and the debug output shows the model selected all 200 rows, but still no data appears in the View...

As far as I can tell from examples this code should work. Is there some property of QTableView or QSqlTableModel that could be causing the problem?

Thanks for any help!

Edit: I should probably mention this is on qt5

Edit: This must have been a Qt or Qt Designer bug... I went back into designer and "morphed" the QTableView into a QTreeView, then switched it back and now all of a sudden it is showing the data... I did not change any of the code when I did this... wtf? If it is a bug I'm not sure I can reproduce it again...