TL; DR
I want to know if there’s a significant difference, when writing Anki add-ons, between (1) calling all Qt functions through the aqt
Python module and (2) using the PyQt5
module.
The context
Today I’ve been downloading some packages and inspecting their source code so that I can get familiar with Qt and write my own add-ons. I have a question regarding this topic.
I’ve managed to write the following minimal working example. The following code makes Anki show a button called My action
in Tools
in the menu bar. When My action
is clicked, a dialog is opened with a single button called Help
which, when pressed, calls my_function
.
import aqt
from PyQt5 import QtCore, QtWidgets
class my_form():
def setup_ui(self, Dialog):
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.MinimumExpanding
)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setEnabled(True)
self.buttonBox.setSizePolicy(sizePolicy)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Help)
self.retranslate_ui(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslate_ui(self, Dialog):
translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(translate("Dialog", "editing_fields"))
class my_dialog(QtWidgets.QDialog):
def __init__(self):
QtWidgets.QDialog.__init__(self, parent=aqt.mw)
self.mw = aqt.mw
self.form = my_form()
self.form.setup_ui(self)
self.setWindowTitle(_("The title of my window"))
# Connect button to functions
self.form.buttonBox.helpRequested.connect(self.my_function)
# Resize window
self.resize(100, 100)
# This is mandatory. Otherwise, the dialog won't be shown.
self.exec_()
def my_function(self):
aqt.utils.showInfo('A message from my_function')
my_action = aqt.qt.QAction("My action", aqt.mw)
my_action.triggered.connect(my_dialog)
aqt.mw.form.menuTools.addAction(my_action)
I’ve managed to get the same result only by using the aqt
module (please see code block below)
import aqt
class my_form():
def setup_ui(self, Dialog):
sizePolicy = aqt.qt.QSizePolicy(
aqt.qt.QSizePolicy.Expanding,
aqt.qt.QSizePolicy.MinimumExpanding
)
self.buttonBox = aqt.qt.QDialogButtonBox(Dialog)
self.buttonBox.setEnabled(True)
self.buttonBox.setSizePolicy(sizePolicy)
self.buttonBox.setOrientation(aqt.Qt.Horizontal)
self.buttonBox.setStandardButtons(aqt.qt.QDialogButtonBox.Help)
self.retranslate_ui(Dialog)
aqt.QMetaObject.connectSlotsByName(Dialog)
def retranslate_ui(self, Dialog):
translate = aqt.QCoreApplication.translate
Dialog.setWindowTitle(translate("Dialog", "editing_fields"))
class my_dialog(aqt.qt.QDialog):
def __init__(self):
aqt.qt.QDialog.__init__(self, parent=aqt.mw)
self.mw = aqt.mw
self.form = my_form()
self.form.setup_ui(self)
self.setWindowTitle(_("The title of my window"))
# Connect button to functions
self.form.buttonBox.helpRequested.connect(self.my_function)
# Resize window
self.resize(100, 100)
# This is mandatory. Otherwise, the dialog won't be shown.
self.exec_()
def my_function(self):
aqt.utils.showInfo('A message from my_function')
my_action = aqt.qt.QAction("My action", aqt.mw)
my_action.triggered.connect(my_dialog)
aqt.mw.form.menuTools.addAction(my_action)
The question
Is there any significant difference between these two ways of writing add-ons in Python?
- call everything through the
aqt
Python module - import relevant modules from the
PyQt5
module
I’m more willing to call everything through the aqt
Python module, since most of the time I need to press less key when writing source code.