Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to display icons in QListView without text?

Tags:

c++

qt

qlistview

Using a QListView, and QStandardItemModel, is it possible to display icons in the list view without displaying the associated text? QStandardItem is defined as so:

QStandardItem ( const QIcon & icon, const QString & text ) 

So it seems to require a text string of some sort - I only want the icon displayed. If I use the following code, I get the icons as requested, but I also get a blank text element underneath them. I don't want this.

ImageListView->setViewMode( QListView::IconMode );
{
     QStandardItemModel *iStandardModel = new QStandardItemModel(this);
     QStandardItem* item1 = new QStandardItem(QIcon("images/shield-280x280.png"),"");
     QStandardItem* item2 = new QStandardItem(QIcon("images/shield-280x280.png"),"");

     iStandardModel->appendRow(item1);
     iStandardModel->appendRow(item2);
     ImageListView->setIconSize(QSize(100,100));
     ImageListView->setUniformItemSizes(true);
     ImageListView->setDragDropMode(QAbstractItemView::DropOnly);
     ImageListView->setModel(iStandardModel);
}

If I go to the trouble of building a custom model, can I resolve this issue?

like image 908
Chris K Avatar asked Apr 08 '10 04:04

Chris K


People also ask

What is a qlistview in Qt?

A QListView presents items stored in a model, either as a simple non-hierarchical list, or as a collection of icons. This class is used to provide lists and icon views that were previously provided by the QListBox and QIconView classes, but using the more flexible approach provided by Qt's model/view architecture.

How do I create a list without a qlistwidget?

Alternatively, list items can also be created without a parent widget, and later inserted into a list using QListWidget::insertItem (). List items are typically used to display text () and an icon (). These are set with the setText () and setIcon () functions.

How do I display icons in Windows Forms listview?

The Windows Forms ListView control can display icons from three image lists. The List, Details, and SmallIcon views display images from the image list specified in the SmallImageList property. The LargeIcon view displays images from the image list specified in the LargeImageList property.

What is the difference between list view and largeicon view?

The List, Details, and SmallIcon views display images from the image list specified in the SmallImageList property. The LargeIcon view displays images from the image list specified in the LargeImageList property. A list view can also display an additional set of icons, set in the StateImageList property, next to the large or small icons.


2 Answers

To expand on the accepted answer, here's the simplest delegate which can optionally hide the text (display role) of items, but otherwise acts like the default delegate. This works with any QAbstractItemView subclass (and QComboBox) and any QAbstractItemModel subclass as well. And is a better solution if one would rather keep the display role non-null for other views (or whatever reason).

class ItemDelegate : public QStyledItemDelegate
{
  public:
    using QStyledItemDelegate::QStyledItemDelegate;

    // simple public member to toggle the display role (create getter/setter if you prefer)
    bool displayRoleEnabled = false;

  protected:
    void initStyleOption(QStyleOptionViewItem *o, const QModelIndex &idx) const override
    {
      QStyledItemDelegate::initStyleOption(o, idx);
      // to hide the display role all we need to do is remove the HasDisplay feature
      if (!displayRoleEnabled)
        o->features &= ~QStyleOptionViewItem::HasDisplay;
    }
};
like image 200
Maxim Paperno Avatar answered Oct 08 '22 09:10

Maxim Paperno


Yes, you can do.

first you create a delegate associated with the list-view.Then,

While inserting the elements to the listview, use set-data function to insert the icon and in the paint event of delegate you handle the drawing icon. i hope its clear.

like image 29
Naruto Avatar answered Oct 08 '22 08:10

Naruto