0
votes

I've a list of QListWidgetItem in QListWidget(Hor scroll bar is there as num of items is huge). Each QListWidgetItem contains a QPixmap as a data(scaled down to some random value). My requirement is when a QPixmap is clicked that should be highlighted(rounded rect of brushwidth 10). I'm delegating each QListWidgetItem to a QItemDelegate. I've couple of questions here.

  1. How to Paint the rounded rect of QPixmap when the corresponding QListWidgetItem is selected?

  2. The above mentioned paint event should occur when QPixmap is clicked(not in the other parts of QListWidgetItem). As QPixmaps are of different sizes, top and bottom part of QPixmaps in QListWidgetItem will be empty and clicking there also will trigger the ItemDelegate. How to get rid of this selection?

1

1 Answers

0
votes

I don't know if it's possible with a QListWidget because I've never done it. However, I do it with QListView and custom model and a delegate. The gist of it goes like so:

Inside my custom QAbstractListModel:

QVariant data(const QModelIndex &index, int role) const
{
    if(index.isValid())
    {
        switch(role)
        {
            case MyCustomRole:
                return QVariant(*pointerToMyQPixmap);
            break;
...

Inside my custom QStyledItemDelegate:

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

...

    // Best make DAMN sure it's not null else we will crash and burn.
    QPixmap pix = index.data(myCustomRole).value<QPixmap>();
    painter->drawPixmap(1, 1, pix);

...

In my view, Qt's Model/View framework is kinda lame but until they come to their senses, it's something you'll have to learn if you want anything but the basic features of the built-in widgets they provide in the framework. In other words, if you're wanting more functionality with the QListWidget, you'll need to study up on the QListView and implement your own model and view

http://qt-project.org/doc/qt-4.8/qlistview.html#details