Creating the application icon

We created our sample window, and now we go on to customize it with some features fitting our needs. For each customization, we add a new function under the SampleWindow class in the previous program to define its properties, and we call this our main function to apply these properties on the sample window. In this section, we define an icon to be set on the window that we created. An icon is a small image that is created to visually emphasize the purpose of the program. It is displayed in the top-left corner of the application window. The same is also displayed in the taskbar when the application is minimized. As a prerequisite for this program, you may need an icon image with dimensions similar to the image that is used here (72 X 72). You can create your own image or download it from the book's website if you wish to use the one used in this program:

# Import required modules
import sys, time
from PySide.QtGui import QApplication, QWidget, QIcon


# Our main window class
class SampleWindow(QWidget):
    # Constructor function
    def __init__(self):
        super(SampleWindow, self).__init__()
        
        self.initGUI()

    def initGUI(self):
        self.setWindowTitle("Icon Sample")
        self.setGeometry(300, 300, 200, 150)

    # Function to set Icon
        appIcon = QIcon('pyside_logo.png')
        self.setWindowIcon(appIcon)

        self.show()
        
if __name__ == '__main__':
    # Exception Handling
    try:
        myApp = QApplication(sys.argv)
        myWindow = SampleWindow()
        myApp.exec_()
        sys.exit(0)
    except NameError:
        print("Name Error:", sys.exc_info()[1])
    except SystemExit:
        print("Closing Window...")
    except Exception:
        print(sys.exc_info()[1])

The preceding program only requires the QApplication, QWidget, and QIcon import classes. As the best practice, we are loading only these required classes into our application in the import statement. Coming back to the preceding program, we included QIcon and setWindowIcon to set the application icon from the 'pyside_logo.png' file, and we call these functions from our initGUI function to set it.

As we are not specifying a file path, remember to place the image in the same location as the program. On executing this program, we will get the output as shown in the following screenshot:

Creating the application icon

As we have just seen the basics of setting an application icon, we will move on to explore more about the PySide.QtGui.QIcon class. This class provides a set of functions that provides scalable icons in different modes and states. Using this class, we can create various types of icons differing in their size and mode, namely, smaller, larger, active, and disabled from the set of pixmaps that is given. Such pixmaps are used by the Qt widgets to show an icon representing a particular action.

The QIcon class has the following different forms of constructors:

Qicon()
QIcon(QIconEngine * engine)
QIcon(QIconEngineV2 * engine)
QIcon(const QIcon & other)
QIcon(const QPixmap & pixmap)
QIcon(const QString & fileName)

The constructors in the preceding code are explained as follows:

  • The first form constructs a null icon.
  • The second and third form takes PySide.QtGui.QIconEngine as a parameter. These classes provide an abstract base class for the QIcon renderers. Each icon has a corresponding engine that has the responsibility to draw the icon with the requested size, mode, and state. The third QIconEngineV2 extends QiconEngine with steaming capability and will be the standard engine starting from Qt4.8.
  • The fourth form simply copies from the other QIcon object, and it is considered to be the fastest method of all.
  • The fifth form constructs the icon from the PySide.QtGui.QPixmap class. This class is an off-screen image representation that can be used as a paint device. A pixmap can be easily displayed on the screen using PySide.QtGui.QLabel or one of the two button classes, PySide.QtGui.QPushButton or PySide.QtGui.QToolButton. QLabel has a pixmap property, whereas QPushButton/QToolButton has an icon property.
  • The last form constructs an icon from the given filename. If the filename contains the relative path, it must be relative to the runtime working directory.

Icons are not only used for showing as application icon but also in various places as tool representation in the toolbars. Consider this, we are creating a toolbar in our application where we display icons to represent functionalities in pictorial form. A sample toolbar may appear like the one that is shown in the following screenshot:

Creating the application icon

The QIcon class provides various modes to display the icon by the state it is defined as using the pixmap function applied to the QIcon class. The syntax of the pixmap function is PySide.QtGui.QIcon.pixmap(width, height[, mode=Normal[, state=Off]]). The parameters width and height represent the icon size. The modes can be any of the following four modes in the table depending on the action:

Constant

Description

QIcon.Normal

This displays the pixma p when the user is not interacting with the icon, but the functionality that is represented by the icon is available

QIcon.Disabled

This displays the pixmap when the functionality that is represented by the icon is not available

QIcon.Active

This displays the pixmap when the functionality that is represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it

QIcon.Selected

This displays the pixmap when the item that is represented by the icon is selected

The state parameter can be used to describe the state for which pixmap is intended to be used. It can take any of the following two values in the table:

Constant

Description

QIcon.Off

This displays the pixmap when the widget is in an off state

QIcon.On

This displays the pixmap when the widget is in an on state

The following function will provide you with an example of various modes of icons that we create from setting the modes in the pixmap function. Add the following function from the previous program inside the SampleWindow class:

 def setIconModes(self):
        myIcon1 = QIcon('pyside_logo.png')
        myLabel1 = QLabel('sample', self)
        pixmap1 = myIcon1.pixmap(50, 50, QIcon.Active, QIcon.On)
        myLabel1.setPixmap(pixmap1)
        myLabel1.show()

        myIcon2 = QIcon('pyside_logo.png')
        myLabel2 = QLabel('sample', self)
        pixmap2 = myIcon2.pixmap(50, 50, QIcon.Disabled, QIcon.Off)
        myLabel2.setPixmap(pixmap2)
        myLabel2.move(50, 0)
        myLabel2.show()

        myIcon3 = QIcon('pyside_logo.png')
        myLabel3 = QLabel('sample', self)
        pixmap3 = myIcon3.pixmap(50, 50, QIcon.Selected, QIcon.On)
        myLabel3.setPixmap(pixmap3)
        myLabel3.move(100, 0)
        myLabel3.show()

Now, add the following line in the initGUI function to call this function:

...
        self.setWindowIcon(appIcon)
        self.setIconModes()
        self.show()
...

Note

You may have noted that a new QLabel widget was used in this program. The QLabel widget is used to provide a text or image display.

Running this program will output a window containing different modes of the same icon, as shown in the following screenshot:

Creating the application icon
..................Content has been hidden....................

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