I'm very new to PyQt, so it's possible I'm doing this entirely wrong. I'm currently struggling to set the title
property of a QMessageBox
during instantiation in PyQt6. Here is my code:
import PyQt6.QtWidgets as Qtw
app = Qtw.QApplication([])
alert_window = Qtw.QMessageBox(text="Foo", title="Bar")
alert_window.exec()
When running this, I get the following error:
TypeError: 'title' is an unknown keyword argument
However, __init__
for QMessageBox
(located in the Python stub QtWidgets.pyi
) seems to clearly define title
as a string argument, which I am passing correctly:
@typing.overload
def __init__(self, icon: 'QMessageBox.Icon', title: str, text: str, buttons: 'QMessageBox.StandardButton' = ..., parent: typing.Optional[QWidget] = ..., flags: QtCore.Qt.WindowType = ...) -> None: ...
I've tried to run this by removing the title
keyword argument and just leaving text
and it seems to work just fine. Am I missing something entirely, or is the .pyi
file incorrect? And, if this file is incorrect, is there any other documentation for PyQt that I can reference to find which arguments I can and cannot use? Thanks.
You have two issues:
def function(positional1, positional2, named1=something, named2=whatever):
The above means, simply put, that function
:
positional1
and positional2`);named1
and named2
);The positional arguments:
The named arguments:
=
sign;function(x, y, z)
means that named1
will be equal to z
within the function);function(x, y, named2=z, named1=w)
);The above means that calling function(x)
will be correct, while function(positional1=x)
will not.
Almost all QObjects accept named arguments that correspond to the object's properties, even if they're not declared in the constructor's definition.
For instance, all QWidgets support the enabled
property, meaning that you can do the following:
button = QPushButton(enabled=False)
Why this doesn't work?
Qtw.QMessageBox(text="Foo", title="Bar")
That's for two reasons:
title
argument listed in the constructor is just a positional argument, so, as explained above, using a named argument title="Bar"
is wrong;title
is not listed in QMessageBox property list (nor in its inherited classes);Why this does work?
Qtw.QMessageBox(text="Foo")
This is for two reasons:
text()
is a valid Qt property of QMessageBox;pyi
stubs/definitions as reference; use the documentation, starting with the official C++ API (which is 99.9% the same as it's for Python) and eventually check the interactive Python console using help(class.function)
for arguments and return values;*args
and **kwargs
);