diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py
index b9e6ba626f..d8ea501c9b 100644
--- a/src/calibre/gui2/library/delegates.py
+++ b/src/calibre/gui2/library/delegates.py
@@ -7,6 +7,7 @@ __docformat__ = 'restructuredtext en'
import os
import sys
+from contextlib import suppress
from datetime import datetime
from qt.core import (
@@ -208,8 +209,20 @@ class StyledItemDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
e = self.create_editor(parent, option, index)
+ if (book_id := index.data(Qt.ItemDataRole.UserRole)) and isinstance(book_id, int):
+ setattr(e, 'underlying_book_id', book_id)
return e
+ def setModelData(self, editor, model, index):
+ # Refresh the index using the underlying book_id in case the book list
+ # was changed while the editor was open, for example, by auto add
+ if book_id := getattr(editor, 'underlying_book_id', 0):
+ if (db := getattr(model, 'db', None)) and callable(getattr(db, 'row', None)):
+ with suppress(Exception):
+ row = db.row(book_id)
+ index = model.index(row, index.column(), index.parent())
+ super().setModelData(editor, model, index)
+
def setEditorData(self, editor, index):
# This method exists because of the ignore_kb_mods_on_edit flag. The
# flag is cleared after the editor data is set, in set_editor_data. It
diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py
index 28c382e8ef..973211ed33 100644
--- a/src/calibre/gui2/library/models.py
+++ b/src/calibre/gui2/library/models.py
@@ -1179,6 +1179,8 @@ class BooksModel(QAbstractTableModel): # {{{
elif role == Qt.ItemDataRole.FontRole and self.styled_columns:
cname = self.column_map[index.column()]
return self.styled_columns.get(cname)
+ elif role == Qt.ItemDataRole.UserRole:
+ return self.id(index)
# elif role == Qt.ItemDataRole.ToolTipRole and index.isValid():
# if self.column_map[index.column()] in self.editable_cols:
# return (_("Double click to edit me
"))