| Home | ![]() |
This example provides a simple GUI for converting icons from .ICO format to any other image format supported by Qt. You can also create an .ICO file containing any number of images from other image files.
It demonstrates how to use the QtICOImageFormat class to read and write multiple images. It also shows how to install the QImageIO format handler so that the images in .ICO files can be manipulated using Qt's standard image functions.
To use the example to convert icons to other image formats, open an .ICO file using the "File|Open Icon File" menu entry, select the images to export, and use the "File|Save As" menu entry to open a save dialog for each image.
To convert a number of image files to icons for a .ICO file, use the "Open Image Files" menu entry to import images, select the required images in the icon view, and use the "File|Save As" menu entry to save a new .ICO file.
IcoBrowser::IcoBrowser()
: QMainWindow( 0, "icobrowser_mainwindow"), mode(IconMode)
{
// install the ico format as a image io handler for Qt
QtICOImageFormat::installIOHandler();
setIcon(QPixmap("icobrowser.ico"));
The I/O handler is installed, so now the application icon can be set directly from the .ICO file.
fileOpenIconAction = new QAction(QPixmap(icons[3]), "&Open Icon File ...", CTRL+Key_I, this, "openicon");
fileOpenImageAction = new QAction(QPixmap(icons[2]), "&Open Image Files...", CTRL+Key_P, this, "openimage");
fileSaveAsAction = new QAction(QPixmap(icons[1]), "&Save As", CTRL+Key_S, this, "save");
Because .ICO files can contain multiple images, we can store all of the
images for the tool bar in one file.
while(item) {
if (item->isSelected()) {
QImage image = images[item->text()];
// If we want the icon to have a mask then it needs to contain
// an aplha buffer. For this example we say that all images should
// have a mask.
if (!image.hasAlphaBuffer()) {
QPixmap pm = image;
if (!pm.mask())
pm.setMask(pm.createHeuristicMask());
image = pm;
}
list.append(image);
}
item = item->nextItem();
}
bool result = QtICOImageFormat::write(fn, list);
For each selected image, we first check that it has an alpha mask before appending it to a QValuesList of QImages. The first QImage in the list will be the default icon in the .ICO file. Finally, we write out the image list.
| Copyright © 2003-2006 Trolltech | Trademarks | Qt Solutions
|