[PyQt] what is the difference between QMenu() and QMenu(self)

Algis Kabaila akabaila at pcug.org.au
Sat Jul 30 00:20:27 BST 2011

On Sat, 30 Jul 2011 08:18:58 AM Jian Ma wrote:
> Hi all,
> I created a QMenu object in a class. When I created it with menu =
> QMenu(self) and run the program, it sometimes got the "segmentation fault"
> after exit the program. When I created the object with menu = QMenu(), it
> didn't get this problem. I guess the reason is about the QMenu object
> ownership. In one case, the object is owned by PyQt. In other case, it is
> owned by Qt. I am not sure my guess. Can someone give some explanation?
> Below is some code segment.
> class DnDMenuListWidget(QListWidget):
>     ...
>     ...
>     def dropEvent(self, event):
>         ...
>         ...
>         menu = QMenu(self)    # using QMenu(self)
>         # menu = QMenu()      #  using QMenu()
>         ...
>         ...
> Thanks!

In Python in a class instantiation the instance name is automatically passed 
in a parameter, usually in a named variable "self", so if QMenu is a method of 
a class, a varible named "self" needs to be specified, hence QMenu(self).

If Qmenu is used as a function outside of a class creation, the "self" 
argument is not required, so for functions, hence QMenu().

For further information read up on class creation of Python (and class 
instantiation and class instances).

Good luck,

More information about the PyQt mailing list