How to get icon from any file?



  • QMimeDatabase mime_database;
    QIcon icon;
    QList<QMimeType> mime_types = mime_database.mimeTypesForFileName("file.txt");
    icon = QIcon::fromTheme(mime_types[0].iconName());
    if (icon.isNull())  qDebug() << "null";
    qDebug() << mime_types[0].iconName();
    

    1.This above not works (print : null "text-plain")
    2.How get icon of Exe file? (not mime but specified for file)



  • Hi, for 2. there's an example here



  • 2 works, thanks!
    But why 1 icon isNull() ?

    1. Is possible 1 and 2 under Linux? Linux has something like mime or/and Elf binaries can holds icons?

    Bad:

    QPixmap extractMime(const QString &sourceFile)
    {
        QMimeDatabase mime_database;
        const QString nativeName = QDir::toNativeSeparators(sourceFile);   
        QIcon icon;
        QList<QMimeType> mime_types = mime_database.mimeTypesForFileName(nativeName);
        icon = QIcon::fromTheme(mime_types[0].iconName());
        if (icon.isNull())  qDebug() << "null";
        qDebug() << mime_types[0].iconName();
        return icon.pixmap(16);
    }
    

    Maybe this work for Linux, but for Windows I need registers. Sample C# application https://www.codeproject.com/Articles/29137/Get-Registered-File-Types-and-Their-Associated-Ico

    for example:
    Computer\HKEY_CLASSES_ROOT.mp3 has
    OpenWithProgIds/WMP11.AssocFile.MP3

    Computer\HKEY_CLASSES_ROOT\WMP11.AssocFile.MP3 has DefaultIcon = %SystemRoot%\system32\wmploc.dll,-732
    wmploc.dll is resources and 732 index of icon?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.