0

I'm testing on how can I improve more developing in pyqt5 UI I'm just curious if there is a shorter way to make a QWidget to a QMainWindow other than this code:

class PayrollMainWindow(QtWidgets.QMainWindow): def __init__(self): super(PayrollMainWindow, self).__init__() PayrollWidget = self.Payroll() self.setWindowTitle("PSA Payroll") self.setCentralWidget(PayrollWidget) self.menuBar = self.menuBar() fileMenu = self.menuBar.addMenu('File') editMenu = self.menuBar.addMenu('Edit') class Payroll(QtWidgets.QWidgets):... 
1
  • Nested classes are a very rare requirement (and, most usually, an unnecessary complication). That said, I'm under the impression that you must create a QWidget (which, btw, doesn't have the trailing 's') subclass in order to add a central widget and its related attributes. That's absolutely unnecessary: just create a generic QWidget instance, set a proper layout for it, and add all widgets to that layout, eventually (if required) creating instance attributes for the main window instance. Besides, only classes and constants have capitalized names, not variables (nor attributes or functions). Commented Jun 29, 2022 at 5:07

1 Answer 1

1

So your code almost demonstrates the standard way, however I would never suggest defining a widget subclass inside the MainWindow definition block. They should both be declared at the global level like so:

class Payroll(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent=parent) ... class PayrollMainWindow(QtWidgets.QMainWindow): def __init__(self, parent=None): super().__init__(parent=parent) payrollWidget = Payroll(parent=self) self.setCentralWidget(payrollWidget) ... 

If your looking for a quicker way and you don't need overwrite any functionality then you could forgo subclassing altogether for example:

mainwindow = QMainWindow() payrollWidget = QWidget(parent=mainwindow) mainwindow.setCentralWidget(payrollwidget) mainwindow.setWindowTitle('PSA Payroll') menubar = mainwindow.menuBar() filemenu = menubar.addMenu('File') editmenu = menubar.addMenu('Edit') 
Sign up to request clarification or add additional context in comments.

7 Comments

I'm afraid that the OP believes that they always need to create a QWidget subclass for the central widget and then create all widgets (and attributes) as children of that instance. Which means that they believe that there should be an easier way, but the fact is that the easier way is to not do that at all.
@musicamante I can't imagine how it could be easier... in 7 lines of code you get a main window with a custom title a menubar and a couple of sub-menus .... and only takes a couple more than that if you are subclassing
I'm not criticizing your answer, I'm just pointing out that the OP may have a wrong understanding of how a QMainWindow subclass could be implemented in a simple way. Most of the times, there's absolutely no need for a QWidget subclass for the central widget, because it's normally a basic QWidget with a layout manager, all "relevant" widgets are then added with references created as instance attributes (just like uic does) and, being the main window the "top level" object in that hierarchy, it's usually better to subclass it, since it allows direct access to signals and slots.
Considering what you (correctly) pointed out, based on your code, that could have been done in 5 lines instead of 7, also avoiding an (usually) unnecessary subclass. Not only: since that Payroll widget would have probably created widgets and methods as its own instance attributes, the result would have become unnecessarily complex, for accessing widgets, signals and functions: the difference between accessing self.someAttribute and payrollWidget.someAttribute or even self.centralWidget().someAttribute. Your answer is not wrong per-se, I'm just pointing out that the original question ->
-> was probably based on the wrong premise. Not only the nested subclass (as you correctly noted) is usually unnecessary and confusing, but a subclass for the central widget is in general pointless. Yes, there are cases for which a subclass might be necessary (for instance, a system that allows switching between MDI, single-window and/or tabbed interfaces), but those are very specific, uncommon and actually very complex situations that only experienced developers normally face. The OP clearly is not at that point yet.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.