0

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 Answers1

0

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

  • Thanks for the reply. I also did something like this in ListWidget. While painting the modified Qpixmap into listItem in the Delegate:: paint() method, we need to give co-ordinates where QPixmap is drawn. My question is how to find those co-ordinates as my QPixmap is center aligned. option.rect().x() and y()gives top-left coordinate value of the item. Is there any way to paint center aligned? or do we need to calculate manually? – Gokulakrishnan Gopalakrishnan Mar 08 '12 at 07:03
  • I'm having trouble figuring out what you're doing :-) Perhaps you could be more specific with what you're doing with the QPixmap and why you need coordinates for whatever. –  Mar 08 '12 at 10:52
  • Let me tell my actual issue. In each listItem I'm having a QPixmap. Each QPixmap is of variable sizes. If I click the listItem the corresponding picture should be highlighted in such a way that Rounded rect has to be drawn around that picture. So, in the Delegate paint method, I'm getting the data(QPixmap) and painting the rounded rect around it. Then I want to paint the updated QPixmap on the ListWidgetItem in Center alignment. For painting the QPixmap we need to specify QRect. Anyhow we can manually calculate the geometry for center alignment, but I want to know is there any better approach – Gokulakrishnan Gopalakrishnan Mar 08 '12 at 13:50
  • I've opened a separate thread for center alignment: http://stackoverflow.com/questions/9614586/how-to-center-qpixmap-while-painting-it-inside-qitemdelegate-of-qlistwidget But I wanted to know is there any better approach for highlighting(painting rounded rect) the QPixmap for my requirements – Gokulakrishnan Gopalakrishnan Mar 08 '12 at 13:51