0

it's a Problem of basic OOP understanding i guess. but: Instead of

class MyWidget(QtGui.QWidget):

    def __init__(self):
        """

        Parameters
        ----------

        """
        QtGui.QWidget.__init__(self)
        button = QtGui.QPushButton(self)
        ...

I want my customized Button, inherited like:

class MyButton(QtGui.QPushButton):
    def __init__(self):
    ...

So, i can use it instead:

class MyWidget(QtGui.QWidget):

    def __init__(self):
        """

        Parameters
        ----------

        """
        QtGui.QWidget.__init__(self)
        button = MyButton(self)
        ...

The Problem is, that i don't know how i can make the button being an Object of the Widget, since i can't pass in the widget itself? Thanks in Advance

tobilocker
  • 891
  • 8
  • 27
  • Suggested reading: [Python classes and object oriented programming](http://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/) – Mel Aug 04 '15 at 10:32
  • This: http://stackoverflow.com/questions/3394835/args-and-kwargs/3394902#3394902, may probably helps. – Jean-Sébastien Aug 04 '15 at 16:25

2 Answers2

0

Both case do the same thing: you create button, an instance of a class, in the __init__ function of MyWidget.

Problem is, you won't be able to use button in any other function, because it fell out of scope. You didn't define it as a class attribute, you only defined it as a local variable for this particular function.
To have button as an attribute of MyWidget, you have to use the keyword self:

self.button=MyButton()

Then later on you will be able to do something like myWidget.button.someFunction... You'll also be able to call self.button in any other method of MyWidget.


Some of you confusion might comes from PyQt.

button = QtGui.QPushButton(self)

With the code above, you're passing as an argument self (in your case, self refers to myWidget). This is really specific to Qt, and the purpose is to set myWidget as the parent of button.

self.button = QtGui.QPushButton(self) can seem redundant, but the first self is for python, and the second for Qt (which is C++).


Finally, you should subclass with a parent parameter:

class MyWidget(QtGui.QPushButton):
    def __init__(self, parent= None):
        super(MyWidget,self).__init__(parent)
Mel
  • 5,837
  • 10
  • 37
  • 42
  • i think i didn't get quite to the point, which is: i want the button to be shown in the widget. Works with QPushbutton Object but not with the customized QPushbutton, since the passed in "self" is missing. I could fix this by adding *args to MyButton's init. But thanks anyway – tobilocker Aug 04 '15 at 12:35
  • If you subclass as I showed, you can pass `self` as an argument. Also, you could try to use layout, it will attribute a parent to widgets automatically. – Mel Aug 04 '15 at 12:39
0

Works like this:

class MyPushButton(QtGui.QPushButton):
    def __init__(self, *args):
        QtGui.QPushButton.__init__(self, *args)

To really understand this i probably would find the answer in PyQt reference

tobilocker
  • 891
  • 8
  • 27
  • its not about args and kwargs but more how Qt Objects handle an object self being passed in as argument with *args. but thanks anyway – tobilocker Aug 05 '15 at 11:07