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.
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.
def_init_(self, labelText=QString(), position=LEFT, parent=None):
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?
Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.