I use QTableView
and custom model, and I want scroll to specific item after
model update.
I create two buttons "Update model" and "scroll to":
btn->setText("Update model");
QObject::connect(btn, &QPushButton::clicked, [&tbl_model, view] {
tbl_model.update();
auto idx = tbl_model.index(49, 0);
qDebug() << "idx: " << idx;
view->scrollTo(idx, QAbstractItemView::PositionAtCenter);
});
btn->setText("scroll to");
QObject::connect(btn, &QPushButton::clicked, [view, &tbl_model] {
auto idx = tbl_model.index(49, 0);
qDebug() << "idx: " << idx;
view->scrollTo(idx, QAbstractItemView::PositionAtCenter);
});
update method code:
void update() {
beginResetModel();
auto new_size = data_.size() == 100 ? 50 : 100;
data_.clear();
for (int i = 0; i < new_size; ++i) {
data_.append(i + 1);
}
endResetModel();
}
If I press "Update model" and my model size expanding from 50 to 100, then I see item with row==49 at the bottom of window, then if I press "scroll to" button, I will see it the center.
So how should I use scrollTo
after model full update?
Of course I could add processEvents
or use QTimer::singleShot
,
but it looks like hack, may be there is some event or signal that
view ready for scrolling?