4
\$\begingroup\$

I have a widget like below:

class MainWidget(QWidget):
    #This is grabbed by another function for the name of the page
    name = "First Page"

    def __init__(self, parent, sidebarWidget, customModulesLen):
        QWidget.__init__(self, parent=parent)
        self.customModulesLen = customModulesLen #How many pages there are total
        self.parent = parent #The parent QStackedWidget
        self.sidebarWidget = sidebarWidget #The progress sidebar

        #This the only real content so far, a button to go to the next page
        lay = QVBoxLayout(self)
        pushButton = QPushButton("Next")
        lay.addWidget(pushButton)
        pushButton.clicked.connect(self.onNext)

    def onNext(self):
        """Iterate forward in the QStackedWidget, and update the progress bar"""

        if self.parent.Stack.currentIndex() < self.customModulesLen:
            activeMenu = self.sidebarWidget.menuContainer.findChildren(QLabel)[self.parent.Stack.currentIndex() + 1]
            activeMenu.setPalette(self.sidebarWidget.activeMenuPalette)
            previousMenu = self.sidebarWidget.menuContainer.findChildren(QLabel)[self.parent.Stack.currentIndex()]
            previousMenu.setPalette(self.sidebarWidget.inactiveMenuPalette)

        self.parent.Stack.setCurrentIndex((self.parent.Stack.currentIndex() + 1) % (self.customModulesLen + 1))

The above widget is a page that I'm importing and using for a QStackedWidget. It works just fine, and doesn't appear to be against any major conventions to me atleast.

The problem is that I've never really done this before in PyQT, and I'm having some difficulty determining if this is a good approach at accomplishing this, or if there's some shortcuts or general good idea's I'm missing.

\$\endgroup\$
2
\$\begingroup\$
  • name = "First Page": you retrieve the content of this variable from an other function. Functions returns values vary so from a design principle, we can not assign them to class variables which are supposed to be constant/static.
  • When you instantiate MainWidget, you need to inject 3 arguments into it because of the way you defined its __init__(). Arguments are a different level of abstraction, so you must use them with parsimony. In your case, there is a better way to get rid of sidebarWidget and customModulesLen described in the next bullet.
  • You must keep the __init__() clean and outsource the creation of the widegets to an other function that you can call from __init__(). In that function, you can refer to the previous __init__()'s argument which are sidebarWidget and customModulesLen.
  • You forgot to attach the widget to its parent in lay = QVBoxLayout(self). I mean you must code it this way instead: lay = QVBoxLayout(self.parent). Because objects must own their data members, you should write self.lay = QVBoxLayout(self.master)
\$\endgroup\$
  • \$\begingroup\$ Could you elaborate a bit on your second and third bullet point with some documentation or additional information? I thought the whole purpose of __init__() in QT widgets was for creating widgets. I would also love to learn a different way of handling the attributes for the class. \$\endgroup\$ – Joshua Strot Jun 9 '17 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.