简体   繁体   English

Qt在可选列表中显示资源图标

[英]Qt Show Icons from Resource in a selectable list

I have 200+ icons in my resources in my Qt app. 我的Qt应用程序中的资源中有200多个图标。 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. 我资源中的文件是PNG文件,我需要将它们调整为32x32大小并显示给用户,以便用户可以选择一个。 I don't know which component is useful for it and how to iterate through a resource prefix in Qt. 我不知道哪个组件对其有用,以及如何在Qt中遍历资源前缀。

You can use QListWidget to show the images like icons in a list. 您可以使用QListWidget在列表中显示类似图标的图像。 There are also some other possible ways like using QTableView or QListView which require different implementations. 还有其他一些可能的方式,例如使用QTableViewQListView ,它们需要不同的实现。 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. 但是对于更简单的QListWidget ,您应该将其查看模式设置为IconMode ,设置所需的图标大小,然后将资源中的图标逐一添加到列表小部件中。 Suppose that the icons are in the resource with prefix names icon1 , icon2 , ... . 假设图标在资源中带有前缀名称icon1icon2 ,...。 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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM