4

I have a QTableView that's connected to a QAbstractTableModel subclass. I'm editing the data owned by the model in response to user input in a different control (not the QTableView), and then I'm calling a method on the model that emits the dataChanged event:

void CharacterModel::characterChanged(int idx)
{
    emit dataChanged(index(idx, 0), index(idx, (int)kNumColumns));
}

This works, as long as the QTableView is focused. As soon as it's not, it no longer shows the updated data. If I focus it again, it updates immediately. For example, I modified my code that's calling the above method to call setFocus() on the table view right afterwards, and everything is fine - except that my editing control loses focus every time I do anything. Not OK. I have several editing controls, including spin boxes; if I click on the spin box arrow, the FIRST update is displayed (because the table view is still focused), but then the spin box gets focused and all subsequent changes don't get displayed.

Anybody have any idea what's going on? This seems to be extremely well-defined behavior, but I can't find any reference to it anywhere.

Thanks, Aaron

phrixus
  • 81
  • 4

1 Answers1

4

I think I've solved this. Turns out the solution is to call tableView->viewport()->repaint() after the dataChanged event fires. Calling tableView->repaint() doesn't do it.

phrixus
  • 81
  • 4
  • 1
    Wow, this is a pretty lame bug! Attaching to my model's `dataChanged` event and calling what you suggest fixed it for me. – Steve Lorimer Sep 07 '16 at 05:05
  • Qt introduces: "Use viewport->update() to update the contents of the viewport instead of update() as all painting operations take place on the viewport." https://doc.qt.io/qt-6/qabstractscrollarea.html#viewportEvent:~:text=Use%20viewport%2D%3Eupdate()%20to%20update%20the%20contents%20of%20the%20viewport%20instead%20of%20update()%20as%20all%20painting%20operations%20take%20place%20on%20the%20viewport. – patricxian Aug 25 '23 at 07:22