-1

I'm painting a QPixmap inside the QItemDelegate of QListWidget. Each QListWidgetItem is of different dimensions. Inside the

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

function of QItemDelegate, I want to paint QPixmap with center Alignment inside the QListWidgetItem. option.rect() gives the geometry of the item with which I can calculate the center position and paint accordingly, but I want to know the better approach for this alignment.Can some one let me know the answer?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

1 Answers1

2

Do you know the dimensions of your QListWidgetItem? If so, just paint the QPixmap yourself with something like:

painter->drawPixmap(
    (item->width() - pixmap->width()) / 2,
    (item->heigt() - pixmap->height()),
    pixmap
);
laurent
  • 88,262
  • 77
  • 290
  • 428
  • yes, we can calculate the dimenstions od QListWidgetItem by option.rect. The code mentioned by you is what exactly I have mentioned as calculating ourself. I wanted to know is there any better approach for this other than calculating ourself – Gokulakrishnan Gopalakrishnan Mar 08 '12 at 13:45
  • @gokulakrishnangopalakrishnan, better approach? IMO, the best way is to do it yourself so you can control exactly how it's going to look. I wouldn't delegate to the framework for this kind of simple rendering (even for complex ones, I still think that the best way is to do it "manually". It brings a lot less headaches going around framework glitches and limitations). – laurent Mar 08 '12 at 14:43
  • @Laurant thanks. I've one more question (http://stackoverflow.com/questions/9618912/filtering-mouse-click-event-in-qlistwidget). If possible, can you please answer that? – Gokulakrishnan Gopalakrishnan Mar 08 '12 at 15:06