See T6154: Kleopatra: Assert in CertifyCertificateCommand after setting ownertrust of key for an example of this problem.
This problem should really be fixed in the Command base class, so that it doesn't happen for any other concrete commands. The idea is to keep references to the selected key(s) instead of keeping the indexes of the selected key(s) because the indexes will become invalid when the key list model is reset after a refresh of the key cache.