Stuck on QSvgWidget while updating an abandoned add-on

The add-on in question last worked on Anki 2.1.35, and I’d like to get it working with 23.x. My python skills are tic-tac-toe level.

The add-on is trying to inherit from the QSvgWidget class, and is giving a name error. I believe it’s one of the things meant to be imported with “from aqt.qt import *” - meaning, no other import in that file has an asterisk which would conceal the name of the class being imported. So far that makes sense - it seems you’d normally access it with PyQt6.QtSvgWidgets.QSvgWidget or PyQt5.QtSvg.QSvgWidget, and I’m led to believe aqt.qt manages the differences between Qt5 and Qt6.

However, obvious approaches (like trying to import QtSvgWidgets or QtSvg from aqt.qt to see if they’re available) haven’t borne fruit. I had a look at aqt/qt.py in the source code of the last working Anki version, but it’s not clear to me why it worked then, so that hasn’t taught me much unfortunately. If I punch “aqt.qt.” into pycharm, I can see a lot of options in the suggested completions that come from PyQt6/QtCore, or /QtWidgets, or /QtGUI, but not /QtSvgWidgets. I wonder if I’m barking up the wrong tree with aqt.qt, but if so, about all I can think to do is rummage through the other imports to see if something is meant to smuggle the class in.

Unsurprisingly the immediate question is: how exactly do I reference the class correctly? But moreover, I’d like to know how I ought to reach that conclusion. Sadly I expect to have to wrangle more such issues before the add-on works with the current version. Thanks in advance for your thoughts.

Looking at ./qt/aqt/qt/qt6.py, it seems that QtSvgWidgets isn’t one of the classes that is imported under the umbrella, so to speak.

The following will work, at least until Qt7 comes around:

[Edited to use the correct version given by @7ehc7ehc below:]

try:
    from PyQt5.QtSvg import QSvgWidget
except ModuleNotFoundError:
    from PyQt6.QtSvgWidgets import QSvgWidget

[Old incorrect version:]

try:
    from PyQt5 import QtSvgWidgets
except ModuleNotFoundError:
    from PyQt6 import QtSvgWidgets

and then access it as:
QtSvgWidgets.QSvgWidget

3 Likes

Ah. I see.

So far as I can tell from the documentation, the two versions have different submodule names, so for the moment I’m using this:

try:
from PyQt5.QtSvg import QSvgWidget
except ModuleNotFoundError:
from PyQt6.QtSvgWidgets import QSvgWidget

That appears to work. I think. But I’ll definitely keep this in mind, and switch to your exact suggestion if testing on a Qt5 version of Anki raises this issue again. Still a bit confused as to why it worked on a previous version, but I’ll take functioning code. Thank you very kindly for your help.

Okay, I tested it properly on Qt5 (for 23.10), and your version is correct and mine was wrong. Oops. I edited my original response.