I was recently digging a lot in PyQt and doing some staff with it, but suddenly realized , I don't understand some basic staff which I was doing. How class which inherits let's say from QGraphicsView becomes QGraphicsView itself or how it becomes QWidget ? Also as far as I understand when you inherit from something , class just getting all the methods from class you inherit. Here is some code below :
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtCore as QtCore
class First(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(First, self).__init__(parent)
print('hello')
class Window(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.window = 'test'
self.title = 'test'
self.size = (1000, 650)
self.create()
def create(self):
self.setWindowTitle(self.title)
self.resize(QtCore.QSize(*self.size))
self.graphicsWidget = First(self)
self.mainLayout = QtWidgets.QVBoxLayout( )
self.mainLayout.addWidget(self.graphicsWidget)
self.setLayout(self.mainLayout)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window( )
window.setGeometry(500, 300, 800, 600)
window.show( )
sys.exit(app.exec_( ))
For example in this code , I made class called 'First' and it's inheriting from QtWidgets.QGraphicsView and then I am adding it as a widget to mainLayout inside my Window class. Until now, I was taking it as a granted and didn't think about it much, but can somebody explain to me how my mainLayout.addWidget() which accepting only QWidgets , understands that in front of him is QWidget ? Because if you do print(type(First)) it will give <class 'sip.wrappertype'> not a widget. Thank you