This article explains how to use icons with QListView

转自: http://wiki.forum.nokia.com/index.php/CS001339_-_QListView_with_icons

 

Overview

This article explains how to use icons with QListView .

Description

The QListView class is one of the Model/View classes and is part of Qt's model/view framework. QStandardItemModel can be used to show icons and texts in QListView .

 

Solution

Header file

 

QStandardItemModel* iStandardModel;
QListView* iListView;

.cpp file

 

iListView = new QListView(this);
//Creating a standard item model
iStandardModel = new QStandardItemModel(this);
//First item
QStandardItem* item1 = new QStandardItem(QIcon("c://data//Nokia- logo.bmp"),"First Item");
//Second item
QStandardItem* item2 = new QStandardItem(QIcon("c://data//sample.bmp"),"Second item");
//third item without icon
QStandardItem* item3 = new QStandardItem("Without icon item");
//Appending the items into model
iStandardModel->appendRow(item1);
iStandardModel->appendRow(item2);
iStandardModel->appendRow(item3);
//Setting the icon size
iListView->setIconSize(QSize(40,30));
//Setting the model
iListView->setModel(iStandardModel);
//Setting listview geometry
iListView->setGeometry(QRect(0,0,240,320));

Sample application

File:QListViewIconMode.zip

你可能感兴趣的:(This article explains how to use icons with QListView)