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:
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:
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.QIcon
object, and it is considered to be the fastest method of all.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.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:
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:
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 |
---|---|
| |
|
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() ...
Running this program will output a window containing different modes of the same icon, as shown in the following screenshot: