PDA

View Full Version : QListWidget set item spacing dynamically based on font's height



phenoboy
10th March 2015, 11:21
I would like to set item spacing based on QListWidget's font's size. How can I do that ?

phenoboy
12th March 2015, 07:41
solved by subclassing QItemDelegate and overriding sizehint- function:

.cpp


#include "mylistdelegate.h"
#include <QDebug>

MyListDelegate::MyListDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

QSize MyListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QFontMetrics fm(option.font);
QRect rect;

QSize sz=QItemDelegate::sizeHint(option, index);
sz.setHeight(fm.height()*1.5);

return sz;
}


.h


#include <QStyledItemDelegate>
#include <QItemDelegate>

class MyListDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit MyListDelegate(QObject *parent = 0);
virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;



};