Results 1 to 4 of 4

Thread: Multiple inheritance

  1. #1
    Join Date
    Apr 2021
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Multiple inheritance

    Hi to everybody,
    I'm trying to understand multiple inheritance so I create this class:

    Qt Code:
    1. class myRect(QRectF,QWidget):
    2. def __init__(self):
    3. super(QRectF,self).__init__(QRect(20,20, 200,200))
    4. super(QWidget,self).__init__()
    To copy to clipboard, switch view to plain text mode 



    But I get the error:

    super(QRectF,self).__init__(QRect(20,20, 200,200))

    TypeError: QWidget(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'QRect'


    How can make this kind of inheritance?


    Thank you

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Multiple inheritance

    How can make this kind of inheritance?
    It doesn't make any sense to try to create this type of inheritance. Inheritance should obey the "IsA" rule: If something can be said to be a specialization of a more general thing, then it "is a" case of that thing. If you have a Sheep, then a Ewe "is a" more specific version of a Sheep. A QRect "is not a" QWidget, and a QWidget "is not a" QRect so it does not make sense to try to combine these through inheritance to create something that is both a QRect -and- a QWidget.

    I think you are confusing inheritance with aggregation (or composition). If you want to create a class derived from QWidget that "uses a" QRect to describe its shape, then that class should derive -only- from QWidget, and should contain a QRect as a class member. If you need to specify initial coordinates for the QRect, then they should be passed as an argument to or assign in __init__() :

    Qt Code:
    1. class MyRectWidget(QWidget) :
    2. def __init__( self, parent, rect ) :
    3. super().__init__( parent )
    4. self.rect = rect
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 11th May 2021 at 21:30.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2021
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple inheritance

    But if I would an object that has the features of a QWidget and a QRect (as the methods) shouldn't I combine them this way? Why doesn't it make any sense? I've just started with python so please excuse me for these strange questions

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,940
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Multiple inheritance

    But if I would an object that has the features of a QWidget and a QRect (as the methods) shouldn't I combine them this way?
    No, because when you inherit, it means that the new class has all of the properties and methods of both superclasses, not just the ones you use. There are a lot of QRect methods that are not allowed for a QWidget:

    QRect can be copied (assigned) to another QRect, QWidget cannot.
    QRect can be initialized from another QRect instance, QWidget cannot.
    QWidget has a parent-child relationship with other QWidget instances, QRect does not

    and more. Those are just some of the ones that would cause exceptions or create memory problems in your program.

    Inheritance is not something you do because you need some of the properties of one class along with another. Inheritance generally means that any time you can use one of the superclasses, you can also use the derived class. You can't use your "myRect" class any place you can use a QRect because of the restrictions I gave above. Python would probably throw an exception the first time you tried to assign one myRect instance to another, for example.

    But the bottom line reason is that you design almost certainly does not need multiple inheritance from a class that defines a visible object on screen that draws itself and that you can interact with (QWidget) and a class that simply holds the coordinates of a rectangle and has methods for retrieving the position, size, and other properties.

    Your design might be based on a QWidget with a rectangular shape. So you add a QRect as a member variable for the MyRectWidget class, override the paintEvent(), and in that event call painter.drawRect() and pass your QRect variable. QPainter will draw it with the current pen and fill it with the current brush. You also override the QWidget rect(), pos(), and other methods to return the proper values from your QRect member variable.

    You need to really think about what inheritance means. That's why I described the "IsA" rule. If you can't use a derived class any place you could use the superclass, then it does not obey the "IsA" rule. (A variation of this rule is the "duck test": if it looks like a duck, acts like a duck, and walks like a duck, then it is a duck. If it doesn't in some way, then it is something else, but not a duck).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Multiple inheritance of QObjects
    By tescrin in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2013, 22:57
  2. Multiple inheritance with QObject
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2010, 16:40
  3. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  4. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 04:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.