View Full Version : QListWidget set item spacing dynamically based on font's height
phenoboy
10th March 2015, 10:21
I would like to set item spacing based on QListWidget's font's size. How can I do that ?
phenoboy
12th March 2015, 06: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;
};
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.