getting icon from external applications



  • hello dear friends
    i prepare an uninstaller for windows. i need to get icon of every application beside program name in table widget.
    after some googling i write a test code for getting icons but it not worked and my computer not respond to any thing until i kill the process.
    this is my test code.

    void MainWindow::on_pushButton_clicked()
    {
        QFileInfo fin("C:/Program Files/CCleaner/CCleaner.exe");
        QTableWidgetItem *it = new QTableWidgetItem("Hello");
        QFileIconProvider qq;
        QIcon ic = qq.icon(fin);
        ui->tableWidget->insertRow(0);
        it->setIcon(ic);
        ui->tableWidget->setItem(0,0,it);
    }
    

    please help me what i must to do.
    thanks


  • Moderators

    I don't think you can use C:/Program Files/CCleaner/CCleaner.exe directly to create QIcon as it is an executable containing an icon but not just an icon. You need to extract the icon from the executable. I don't know how to do that. I know there is at least one open source exe thumb-nailer for Linux, maybe you can find some inspiration there. And I'm quite sure there is some specification/documentation for the Windows binary format (exe format). And finally you could use an hex editor and open a simple exe with embedded icon to see how it is stored there.


  • Moderators

    maybe this helps. I assume you need a Windows only solution?



  • @mjzarrin

    Hi,

    The QFileIconProvider class provides file icons for the QDirModel and the QFileSystemModel classes according to the Qt Docs.

    If you need only application's icon, following code:

        QFileInfo  fin("C:/Program Files/CCleaner/CCleaner.exe");
        QTableWidgetItem *it = new QTableWidgetItem("Hello");
        QFileSystemModel *model = new QFileSystemModel;
    
        model->setRootPath(fin.path());
    
        QIcon ic = model->fileIcon(model->index(fin.filePath()));
    
        ui->tableWidget->insertRow(0);
        it->setIcon(ic);
        ui->tableWidget->setItem(0, 0, it);
    
    

    or

    QFileInfo  fin("C:/Program Files/CCleaner/CCleaner.exe");
    QTableWidgetItem *it = new QTableWidgetItem("Hello");
    QFileSystemModel  *model = new QFileSystemModel;
    
    model->setRootPath(fin.path());
    
    QFileIconProvider   *qq = model->iconProvider();
    QIcon  ic = qq->icon(fin);
    
    ui->tableWidget->insertRow(0);
    it->setIcon(ic)
    ui->tableWidget->setItem(0, 0, it);


  • Hello My Friends
    Thank you @jsulm .
    Thank you @raven-worx .
    and thank you @Devopia53 for the best and right solution. It work like a charm. It is what i need.
    thanks.


Log in to reply
 

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