Home > Mobile >  Refreshing QTableView based on data refreshed periodically via QTimer
Refreshing QTableView based on data refreshed periodically via QTimer

Time:09-20

I'm trying to refresh a QTableView based on data from a method get_data which is called periodically by QTimer as below. Once I get the data, I update it in my model (via a member function refresh) and then call refresh_table to hopefully update the tableView with the current data. However, the call to tableView->update() (I've also tried tableView->repaint()) does not work as intended, and tableView does not update - staying put at its first-call value. Note that my data is liable to change completely, and it's not simply a matter of inserting extra rows.

void MainWindow::DataDisplay(QWidget *tab) {
    auto tableView = create_table(tab,...);
    auto timer = new QTimer(this);
    auto model = new MyModel();
    connect(timer, &QTimer::timeout, [tableView, model, this]() {
        const auto data = get_data();
        model->refresh(data);
        refresh_table(model, tableView);

    });
    timer->start(1000);
}
auto refresh_table(MyModel *model,
                   QTableView *tableView) {
    tableView->setModel(model);
    tableView->update(); // does not work
}

What's the canonical way of refreshing one's model in Qt such that the associated TableView is updated?

CodePudding user response:

The canonical way is for your model to emit a dataChanged() signal. Any associated QTableViews will receive that signal and respond by re-querying the model for new values and updating the associated cells of their table.

  • Related