views:

147

answers:

1

I've got the code below, and I was planning on making several classes all within the same "import". I was hoping to instantiate each class and get a return value with the widgets I'm making.

This isn't really a PyQt question at all, more of a "good practices" question, as I'll have a class for each widget.

Should I make functions that return the widgets that were created, if so how? How do I ensure it is difficult to directly instantiate the class if that is the best method for what I'm after?

I'd like to be able to do something like ....

tabs = wqTabWidget( ['firstTab', 'Second', 'Last Tab'] )

or (which ever is a better practice)

tabs = wqInstance.createTabs( ['firstTab', 'Second', 'Last Tab'] )

Here's my class so far....

from PyQt4 import QtCore as qc
from PyQt4 import QtGui as qg

class wqTabWidget(qg.QTabWidget):
    def __init__(self, *args):

        apply(qg.QTabWidget.__init__,(self, ))
        tabList = []
        tabNames = args[0] 

        for name in tabNames:
            tabWidget = qg.QWidget()
            self.addTab(tabWidget,  name)
            tabList.append( { name:tabWidget } )

    print 'hi'


if __name__ == '__main__':
    app = qg.QApplication(sys.argv)
    window = wqTabWidget(['hi',  'there',  'and',  'stuff'])
    window.show()
    app.exec_()
+3  A: 

The answer will be decided if the list of tabs can be changed at runtime. If this widget really only supports adding a set of tabs, but never changing or appending new ones, the list of tabs should come from the initializer. Otherwise you should also add a method to do the job. Consider the QLabel widget which can set the label's text in the initializer and through the setText method.

Other code idea tips.

Your initializer's arguments is a little confusing because you accept an arbitrary number of arguments, but only do something with the first one, and expect it to be a list of strings. A clear list of arguments is important.

Your use of apply to call the base class initializer is unnecessary. Change the code to simply qg.QTabWidget.__init__(self)

When creating a PyQt widget, I almost always prefer to allow a "parent" argument, even when I know the widget is going to be a toplevel widget. This is what all the built in Pyqt methods do, and feels like good practice to follow.

I also can't see the reason to store a list of tabs, with each one being a single element dictionary. I suspect you won't need to keep your own list of tabs and tab names. The QTabWidget can answer all questions about the contents.

If I were to bend this example code to my own preferences it would look like this.

from PyQt4 import QtCore as qc
from PyQt4 import QtGui as qg


class wqTabWidget(qg.QTabWidget):
    def __init__(self, parent, tabNames):
        qg.QTabWidget.__init__(self, parent)
        self.createTabs(tabNames)

    def createTabs(tabNames):
        for name in tabNames:
            tabWidget = qg.QWidget()
            self.addTab(tabWidget,  name)


if __name__ == '__main__':
    app = qg.QApplication(sys.argv)
    window = wqTabWidget(None, ['hi',  'there',  'and',  'stuff'])
    window.show()
    app.exec_()
Peter Shinners