July 5, 2010

rule rule
Lab Rat
5 posts

QListWidget delegate paint problem.

 

Hi all,
A have just made cistom item delegate, and found some problem on implementing cross-platform paint and sizeHint class methods.

I have Big text in my cell, and I want to adjust Height regarding it,” here is the code of delegate. Your text to link here… [pastebin.com]

As you can see, the sizeHint Method:

  1. QSize RaspListDeleagte::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3.     if (!index.isValid()) return QStyledItemDelegate::sizeHint(option,index);
  4.  
  5.     QSize result;
  6.     //setup fonts
  7.     QFontMetrics fmTF(getTimeFont(option));
  8.     result.setHeight(fmTF.lineSpacing()+fmTF.leading()*2+4);
  9.     result.setWidth(option.rect.width());
  10.     return result;
  11. }

And I draw the text by following code:
  1.         painter->setFont(timeFont);
  2.         painter->drawText(option.rect.x()+2,option.rect.y()+fmTF.height(),timeLeft);

I got option.rect.y() plus QFontMetrics height.
It worked good on Mac OS, but it looks ugly on Linux:
pict

As you can see, the big font text with tome shifted (I got shifted option.rect.y()). It mean that every cell of QListWidget has some top marging, ot content margin. But I didn’t find any in QStyleOptionViewItem.

How I can get this margin from Item Delegate ?

4 replies

July 5, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

As a quick answer I can advice you to add some style sheet to your instance of QListWidget with removed margines. I used such way once, but I was in opposite situation – margins were needed in some places.

July 5, 2010

rule rule
Lab Rat
5 posts

Regarding this doc [doc.trolltech.com]: I need “Padding” property. But I did not find any direct class methods.

I’ve just checked, And passed such string in initialization of QWidgetList:

  1.    
  2. ui->listView->setStyleSheet(" QListWidget { padding: 40px;}");

Unfortunately, with no results.

July 5, 2010

Andre Andre
Area 51 Engineer
6031 posts

I have found that custom QItemDelegates can get trickyer than you’d like sometimes…

Perhaps you could work around your issue by not using a delegate, but by using a proxy model. In that model, you could use the item roles for the font, alignment and color to achieve the look you want, and have Qt’s code deal with the actual layout.

Note that this only works if what we are seeing above are separate columns.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 5, 2010

rule rule
Lab Rat
5 posts

I’m already made workaround, by using overloading method of QPainter, to Draw in QRect, instead of x,y.
I have left the sizeHint() in original state, and reimplement paint().
The code now is:

  1.        
  2. painter->drawText(option.rect.adjusted(2,0,fmTF.width(timeLeft),0),timeLeft);

 
  ‹‹ Deploying Phonon on Windows problem      how to creat a different height editer from it’s item In a QItemDelegate? ››

You must log in to post a reply. Not a member yet? Register here!