Creating Composite Widgets

A composite widget is a widget that is composed of two or more other widgets. We are already experienced composite widget creators: For example, every dialog we have created is a composite widget. We are dedicating some space to a topic we have already covered because unlike the dialogs we created earlier (which were QDialog subclasses), we want to create composite widgets that are not dialogs, and that instead can be used inside dialogs (or as a main window's central widget).

The kind of composite widget we want is very similar to a dialog: We create the child widgets, lay them out, and do any signal-slot connections we need. The main difference is that we inherit from QWidget rather than from QDialog.

Figure 11.2 A dialog using labelled widgets

The screenshot in Figure 11.2 shows what looks like a conventional dialog, but in fact we have explicitly created only six widgets rather than twelve. This is because we have used four custom LabelledLineEdits, and one custom LabelledTextEdit, along with a QDialogButtonBox.

Our labelled editors are special in two ways. First, they automatically set up buddy relationships, and second, they can lay out the label either to the left or above their editing widget.

self.zipcode = LabelledLineEdit("&Zipcode:") self.notes = LabelledTextEdit("&Notes:", ABOVE)

Creating labelled editors is easy, as this snippet from the form's initializer shows. The LabelledLineEdit exposes its widgets as instance variables, so we can access its QLabel as LabelledLineEdit.label and its QLineEdit as Labelled-LineEdit.lineEdit.The LabelledTextEdit has the same label, and has a textEdit for its QTextEdit.The ABOVE is just a module constant; there is also a corresponding LEFT.

class LabelledLineEdit(QWidget):

def_init_(self, labelText=QString(), position=LEFT, parent=None):

super(LabelledLineEdit, self)._init_(parent)

self.label = QLabel(labelText)

self.lineEdit = QLineEdit()


layout = QBoxLayout(QBoxLayout.LeftToRight \

if position == LEFT else QBoxLayout.TopToBottom) layout.addWidget(self.label) layout.addWidget(self.lineEdit) self.setLayout(layout)

The preceding code is the complete LabelledLineEdit class. If we want to connect signals and slots to its label or line edit, we can do so by accessing them directly since they are held as public instance variables. Instead of using a QVBoxLayout or a QHBoxLayout, we have used their base class so that we can set the layout direction when the labelled line edit is created. We won't show the code for the LabelledTextEdit since it differs only in that we create a QTextEdit instead of a QLineEdit and call it textEdit instead of lineEdit.

Although we have created the composite labelled editing widgets purely in code, it is possible to create composite widgets using Qt Designer, basing them on the "Widget" template.

Creating composite widgets that are used repeatedly can save time in large projects. They are also useful when we want to create a main-window-style application whose central widget must consist of two or more widgets, and where using an MDI workspace, splitters, or dock windows is not a suitable solution.

Was this article helpful?

0 0


The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook

Post a comment