Updating view with QAbstactTableModel
-
Hello. I'm subclassing
QAbstractTableModel, reimlementedrowCount, columnCount, data, headerData. My model has class-container(Points pointswhich hasset<set<string>>) and method:const TableModel& TableModel::add(const string& s1, const string& s2, const string& s3 ) { auto info = points.prepare_for_add(s1, s2, s3); // first - index, where to put, second - true, if item already exist (update s2, s3) if (!info.second) { beginInsertRows(QModelIndex(), info.first, info.first); points.add(s1, s2, s3); endInsertRows(); } else { points.add(s1, s2, s3); dataChanged(index(info.first, 0), index(info.first, 2)); } return *this; }In
mainWindowI haveQTableViewwith my model, and buttonconnect(loadbutton, &QPushButton::clicked, [=]() { for (...) { model->add(s1, s2, s3); }When i click button, new empty rows are adding until the end of a cycle, and only after the end of the cycle data appears. If I put
view->update()aftermodel->add(), everything is ok, rows are adding immediately with data. Can I somehow do this, without callingview->update()? Maybe I need to change mymodel->add, somehow? Am I using(begin/end)InsertRows()anddataChanged()right? -
Hello. I'm subclassing
QAbstractTableModel, reimlementedrowCount, columnCount, data, headerData. My model has class-container(Points pointswhich hasset<set<string>>) and method:const TableModel& TableModel::add(const string& s1, const string& s2, const string& s3 ) { auto info = points.prepare_for_add(s1, s2, s3); // first - index, where to put, second - true, if item already exist (update s2, s3) if (!info.second) { beginInsertRows(QModelIndex(), info.first, info.first); points.add(s1, s2, s3); endInsertRows(); } else { points.add(s1, s2, s3); dataChanged(index(info.first, 0), index(info.first, 2)); } return *this; }In
mainWindowI haveQTableViewwith my model, and buttonconnect(loadbutton, &QPushButton::clicked, [=]() { for (...) { model->add(s1, s2, s3); }When i click button, new empty rows are adding until the end of a cycle, and only after the end of the cycle data appears. If I put
view->update()aftermodel->add(), everything is ok, rows are adding immediately with data. Can I somehow do this, without callingview->update()? Maybe I need to change mymodel->add, somehow? Am I using(begin/end)InsertRows()anddataChanged()right?Not sure what you mean by "until the end of a cycle". You add rows in a
forloop, and I imagine the view does not get updated (visually) until that has finished. Insertingview->update()after each row add causes the visual updates to be seen immediately at that point.?
-
Not sure what you mean by "until the end of a cycle". You add rows in a
forloop, and I imagine the view does not get updated (visually) until that has finished. Insertingview->update()after each row add causes the visual updates to be seen immediately at that point.?
@JonB yes, without
view-update()only empty rows are added immediately. But afterforcycle, when all empty rows have been added, data appears -
@JonB yes, without
view-update()only empty rows are added immediately. But afterforcycle, when all empty rows have been added, data appears@Kot-Shrodingera
So it seems to me that is the way it works, and you have your optional workaround of explicitly callingview->update()if you want immediate visual feedback. -
@JonB yes, without
view-update()only empty rows are added immediately. But afterforcycle, when all empty rows have been added, data appears@Kot-Shrodingera said in Updating view with QAbstactTableModel:
only empty rows are added immediately
I'm surprised they are. I would have thought you'd need the control to go back to the event loop for anything on the screen to happen.
You could replace:
model->add(s1, s2, s3);
with
QMetaObject::invokeMethod(model,std::bind(&TableModel::add,model,s1,s2,s3),Qt::QueuedConnection);I don't know what
stringis in this context but you might need to declare and register it with Qt's meta object system