Search code examples
c++qtlistviewiconsresource-files

Qt Show Icons from Resource in a selectable list


I have 200+ icons in my resources in my Qt app. I want to know how can I list and show icons to user so user can choose one as user avatar sort of. Files in my resource is PNG files, I need to resize them to 32x32 and show them to user so user can choose one. I don't know which component is useful for it and how to iterate through a resource prefix in Qt.


Solution

  • You can use QListWidget to show the images like icons in a list. There are also some other possible ways like using QTableView or QListView which require different implementations. But for the case of QListWidget which is more simple you should set it's view mode to IconMode, set your desired icon size and add the icons from resource to the list widget one by one. Suppose that the icons are in the resource with prefix names icon1, icon2, ... . Then it can be like :

    ui->listWidget->setViewMode(QListWidget::IconMode);
    
    ui->listWidget->setIconSize(QSize(32,32));
    
    for(int i = 1; i<=200;i++)
    {
       ui->listWidget->addItem(new QListWidgetItem(QIcon(QString(":/res/icon%1").arg(i)),QString("icon%1").arg(i)));
    }