Solved Cannot display newly loaded data
-
Hello,
I had a working code that loaded information from a file and then setModel to cause it to display to screen. But when I added a button to do this, replacing the temporary direct loading, the table view doesn't come up. I do set dataChanged over the whole table, or so I think, on loading. But it doesn't work. I thought that perhaps it wasn't really there, but my add record button increments the total as if it is there but I just can't see it, so I think that it really does exist.Here's my code.
The constructor that sets the model initially (and where I had previously called the file loader that had worked but which now has the intention of not loading right away):
workItem::workItem(QWidget *parent) : QWidget(parent), ui(new Ui::workItem) { ui->setupUi(this); labelTabsWith = PATIENTID; // default selection workItemListViewingModel *sourceModel = new workItemListViewingModel(this); QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(sourceModel); ui->workItemList->setModel(proxyModel); ui->workItemList->verticalHeader()->setVisible(true); ui->workItemList->horizontalHeader()->setVisible(true); ui->workItemList->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); ui->workItemList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); connect(ui->workItemList->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(workItemSelectedForAnalysis(int))); }
Here is my slot that is connected to the load button:
oid workItem::on_listFileNameButton_clicked() { workItemListFileName=QFileDialog::getOpenFileName(this, tr("Work Item List File"), "", tr("Work Item List Files (*_wilist.*)")); workItemListEntries.clear(); if (!loadWorkItemListFromFile(workItemListFileName, workItem::Json)) { std::cerr << "!fromJsonworkItem.loadWorkItem(Json) test tripped" << std::endl; return; } QFileInfo fi(workItemListFileName); ui->listFileNameButton->setText(fi.fileName()); emit reloadListToModel(); };
As you can see, I am emitting a signal to the model class, as what I think is the right way to do it. In fact, the signal is causing the following function of the model class to run as expected:
oid workItemListViewingModel::reloadList() { qDebug() << "into reload list"; emit dataChanged(parent->ui->workItemList->indexAt(parent->ui->workItemList->rect().topLeft()), parent->ui->workItemList->indexAt(parent->ui->workItemList->rect().topLeft())); }
But alas, nothing is displayed in the table view!
Thank you for any help,
Andy[edit: added missing coding tags ```SGaist]
-
Hi,
What does loadWorkItemListFromFile do ?
-
@buckler Some more on this. My attempt to create topLeft and bottomRight index's seems to be failing. It always responds with -1,-1, in both cases. Is there a better wya to do this?
-
@SGaist (Sorry SGaist, I saw your question after positing my last comment, and then the forum held off my further reply to your question for a period.)
loadWorkItemListFromFile reads the data from a file into the data structure connected o the view by the model:
bool workItem::loadWorkItemListFromFile(QString fileName, workItem::saveFormat saveFormat)
{
//QFile loadFile(saveFormat == Json
// ? QStringLiteral(fileName)
// : QStringLiteral("junk_test_wilist.dat"));
QFile loadFile(fileName);if (!loadFile.open(QIODevice::ReadOnly)) {
QErrorMessage *error = new QErrorMessage (0);
error->showMessage(tr("Warning: Cannot open work item list file for loading."));
return false;
}QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc(saveFormat == Json
? QJsonDocument::fromJson(saveData)
: QJsonDocument::fromBinaryData(saveData));readWorkItemList(loadDoc.object());
return true;
} -
I didn't realized: You are trying to get indexes through a point that's the top left of the widget which is outside the region of interest. But in any case, that's completely wrong. You model should not try to access any widget, that's not its role.
I suspect readWorkItemList translates your json to something like a QStringList ?
-
Yes, readWorkItemList does read in a QList from a json file (not a string list, but a list of more complex objects, my "workItems").
In further considering this since your reply, I was trying to understand the expectations of the model approach, it seemed to me that the idea of ripping out and replacing an entire dataset like loadWorkItemList does may not be the intended use case. So I tried a different approach this evening. In between load operations, the model editing, row insertion/deletion, sorting, all works fine. So, on a new load, I have succeeded by freeing up the model as well as its proxy, clearing the underlying QList, then loading a re-doing the setModel call. I now have this working fine, so in that sense, I could mark the issue solved. But I would be interested in your thoughts on this. It seems like you may agree, based on your comments that the signal which is sent to issue a dataChanged call seems to be what you say is wrong. My new method doesn't require this, so maybe I have come to a good solution?
Thoughts welcome, but it is working now so I do not want to use your time inconsiderately.
Andy
-
There's no need recreate the model from scratch and setting it again on the views.
If you replace the complete list it should as simple as:
beginResetModel(); clear old list set new list endResetModel();
and you should be good to go.