Results 1 to 2 of 2

Thread: QListWidget set item spacing dynamically based on font's height

  1. #1
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget set item spacing dynamically based on font's height

    I would like to set item spacing based on QListWidget's font's size. How can I do that ?

  2. #2
    Join Date
    Jun 2012
    Posts
    63
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget set item spacing dynamically based on font's height

    solved by subclassing QItemDelegate and overriding sizehint- function:

    .cpp

    Qt Code:
    1. #include "mylistdelegate.h"
    2. #include <QDebug>
    3.  
    4. MyListDelegate::MyListDelegate(QObject *parent) :
    5. QItemDelegate(parent)
    6. {
    7. }
    8.  
    9. QSize MyListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    10. {
    11. QFontMetrics fm(option.font);
    12. QRect rect;
    13.  
    14. QSize sz=QItemDelegate::sizeHint(option, index);
    15. sz.setHeight(fm.height()*1.5);
    16.  
    17. return sz;
    18. }
    To copy to clipboard, switch view to plain text mode 

    .h
    Qt Code:
    1. #include <QStyledItemDelegate>
    2. #include <QItemDelegate>
    3.  
    4. class MyListDelegate : public QItemDelegate
    5. {
    6. Q_OBJECT
    7. public:
    8. explicit MyListDelegate(QObject *parent = 0);
    9. virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    10.  
    11.  
    12.  
    13. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 5th September 2013, 16:29
  2. Font Height and width based on font size
    By Ghufran in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2010, 09:02
  3. Determining the height of an item in a QListWidget
    By negritot in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2009, 20:18
  4. Change Font of QListWidget to Monospace Font
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 19:23
  5. Dynamically resize spacing
    By trskel in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.