Showing a tooltip

Our next customization is to show a tooltip for the different modes of icons that are shown. Tooltip is handy when you need to display some help text or information to the users. Displaying help text for the widgets that are used in the window is an integral part for any GUI application. We use the PySide.QtGui.QToolTip class to provide tooltips (also called balloon help) for any widget. The QToolTip class defines the properties of the tooltip, such as font, color, rich text display, and so on. As an example, the font properties can be set as follows:

QToolTip.setFont(QFont("Decorative", 8, QFont.Bold))

After setting the font, we set the tooltip to the widget by calling the setToolTip() function provided by the QWidget class:

   myLabel1.setToolTip('Active Icon')

The QFont class specifies a font that has to be used to draw the text. Using the functions that are provided by this class, we can specify various attributes that we want our font to have. If the font type that we specify is not installed or available, Qt will try to use the closest match available. If a chosen font does not include all the characters that need to be displayed, QFont will try to find the characters in the nearest equivalent fonts. When a PySide.QtGui.QPainter draws a character from a font, the PySide.QtGui.Q font will report whether or not it has the character; if it does not, PySide.QtGui.Q Painter will draw an unfilled square.

Modify the previous program as shown in the following code snippet:

     ...    
def initGUI(self):
        self.setWindowTitle("Icon Sample")
        self.setGeometry(300, 300, 200, 150)
        QToolTip.setFont(QFont("Decorative", 8, QFont.Bold))
        self.setToolTip('Our Main Window')
 ...


def setIconModes(self):
...
        myLabel1.setPixmap(pixmap1)
        myLabel1.setToolTip('Active Icon')
        ...        
        myLabel2.move(50, 0)
        myLabel2.setToolTip('Disabled Icon')
        ...        
        myLabel3.move(100, 0)
        myLabel3.setToolTip('Selected Icon')

After making the changes, run the program and you can see that the tooltip text is displayed as and when you move over the icon and window. The sample output is displayed in the following screenshot:

Showing a tooltip
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.202