Home

An Icon Browser

Introduction

This example provides a simple GUI for converting icons from .ICO format to any other image format supported by Qt. You can also create a .ICO files from other image files.

It demonstrates how to use ICO plugin to read multiple images from the same file. Note that you can not write many icons to an ICO file. This is not supported yet. If you try to write many images to the ICO plugin, you might get unexpected results.

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.

Using the plugin

The ICO handler is only available as a plugin, and this enables you to use normal QImage and QPixmap functions to read and write images. For example:

    myLabel->setPixmap(QPixmap("myimage.ico"));

    QPixmap myPixmap;
    myPixmap.save("myimage.ico", "ICO");

If you need to read many images from an ICO file, you need to use the jumpToNextImage functionality of QImageReader, for example you can easliy read many images from the same file into a QList:

    static QList<QImage> readImages(const QString &strFilename)
    {
        QImageReader reader(strFilename);

        QList<QImage> icons;
        do {
            icons << reader.read();
        } while (reader.jumpToNextImage());
        return icons;
    }

The plugin currently only supports saving one image to an ICO file, and the example stores it to disk using QImage::save.

    void IcoBrowser::saveAs()
    {
        bool selected = false;

        int row = 0;
        QListWidgetItem *item = iBrowser->item(row);
        while (item) {
            if ( iBrowser->isItemSelected(item) ) {
                selected = true;
                break;
            }
            item = iBrowser->item(++row);
        }

        if (!selected) {
            QMessageBox::information(this, "ICO Browser",
                "Please select one or more items to save.",
                QMessageBox::Ok);
            return;
        }

        QStringList formats = ConvertList<QString, QByteArray>( QImageWriter::supportedImageFormats() );

        for (int i=0; i<formats.count(); i++)
            formats[i].prepend("*.");

        while(item) {
            if ( iBrowser->isItemSelected(item) ) {
                QString fn = QFileDialog::getSaveFileName(this, QString() ,
                    QString(), "Image File (" + formats.join(" ").toLower() + ")");
                if (!fn.isEmpty() ) {
                    QByteArray extension = fn.right(3).toUpper().toAscii();
                    if (images[item->text()].save(fn, extension.data()))
                        statusBar()->showMessage(item->text() + " saved as " + fn, 2000 );
                    else
                        statusBar()->showMessage(" Failed to save "+ item->text(), 2000 );
                }
            }
            item = iBrowser->item(++row);
        }
    }


Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Solutions