Plajerity
Plajerity

Reputation: 161

Create parent class in Python and pyqt5

I've got a problem with creating parent class for multiple windows in pyqt5

from PyQt5 import QtWidgets, QtCore, QtGui
from Ui_1 import Ui1

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        for key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Down):
            QtWidgets.QShortcut(key, self, partial(self.focusNextPrevChild, True))

class Window1(QtWidgets.QMainWindow, Ui1):
    def __init__(self, parent=None):
        super().__init__()
        self.setupUi(self)
        self.show()

I want the code from MainWindow to work on Window1 objects. I found this example: python pyqt and parent class but I don't get how to use it.

Upvotes: 1

Views: 382

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

It is only necessary that you change QtWidgets.QMainWindow to MainWindow:

from PyQt5 import QtWidgets, QtCore, QtGui
from Ui_1 import Ui1

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        for key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Down):
            QtWidgets.QShortcut(key, self, partial(self.focusNextPrevChild, True))

class Window1(MainWindow, Ui1):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.show()

Upvotes: 1

Related Questions