|GUI Programming with Python: QT Edition|
|Prev||Chapter 13. Actions: menus, toolbars and accelerators||Next|
As you can see, once you have defined your set of QActions, you have little to worry about concerning the definition of menus and toolbars. However, if you want other widgets beyond simple buttons in your toolbars, you cannot use QAction or QActionGroup. One popular addition to a toolbar is a combobox. Adding the combobox means quite a bit of work:
self.fileToolbar.addSeparator() self.labelSelector=QLabel("Font: ", self.fileToolbar) self.comboSelector=QComboBox(self.fileToolbar) self.comboSelector.insertStrList(["Times","Arial","Cyberbit"], 1) self.comboSelector.setEditable(FALSE) self.connect(self.comboSelector, SIGNAL("activated(int)"), self.slotFontChanged) QWhatsThis.add(self.comboSelector,"""Font Selection Select the font that expresses your personality best.""")
First, we give the widget a bit more room by adding a separator. Then we need a label, so people will know what the contents of the combobox represent. Of course, a combobox must be populated with relevant items (font names in this case). We ensure that people can't add items to the list, nor change the names of existing items, by setting editable to false. Finally, the activated signal is connected to a likely slot and a ‘what's this' text is added. The result looks like this:
A toolbar with a combobox added.
That ‘what's this' text leads us to a special button offered by PyQt. This is the little arrow-plus-question-mark that activates the ‘What's This' mode. When the user selects this button, he or she can click on any interface element. A yellow note with a bit of explanation is then shown. This is particularly welcome when your toolbar icons are non-intuitive.
Help with an unhelpful icon
Achieving this demands two steps: actually setting the help text in the QAction, and adding the appropriate button to the toolbar:
If you want to embed a small picture of the icon in the yellow note, you use PyQt's QMimeSourceFactory. The trick is to first set the text to the widget, with an embedded img link pointing towards an identifier in the text. This identifier will be used to look up an image in the application-wide default mime source factory.
You can use source factories for every kind of data for which a mime-type exists, but they are most often used for simple pictures that have to be embedded in rich text. Rich text in Qt is text which is marked up with a subset of html. The <img> tags don't point to an URL, but to the contents of the mime source factory.
self.actions["fileQuit"].setWhatsThis( """<img source="filequit">Quit<br><br> By selecting quit you leave the application. If your document was changed, you will be asked to save it, so it's quite safe to do.""") QMimeSourceFactory.defaultFactory().setPixmap('filequit', QPixmap(filequit))
You can add this code to the initActions() method of the DocviewApp in the document-view framework. Mime source factories are very capable beasts. We can feed the factory just the QPixmap which is derived from the XPM data in the resources.