Results 1 to 9 of 9

Thread: [solved]QListView dynamic item height

  1. #1
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [solved]QListView dynamic item height

    Hi all,
    im trying to implement a listview with model and delegate which items should resize dynamicaly.
    for example if i press an item and its selected the height should be 2*normal height.
    i tried to reimplement sizeHint of QItemDelegate, but the sizeHint isnt catched dynamically. it seems its only used on initialising the view.
    I found no methods in QAbstractItemModel or QItemDelegate or QListView that could handle this.
    Any suggestions?
    Last edited by kernel_panic; 18th February 2009 at 23:11.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [solved]QListView dynamic item height

    Hi kernel_panic,

    how do you solved it? I only know the trick with a QTreeWidget...

    Please can you provide some/the relevant code?


    Thanks

  3. #3
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [solved]QListView dynamic item height

    Hey folks!
    Heres a small example.
    The trick is simply this:
    Qt Code:
    1. const void wantsUpdate(){emit layoutChanged();}
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. mutable int currentIndex;
    2. void DirObjectDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. if (option.state & QStyle::State_Selected)
    6. {
    7. int moi = index.row();
    8. if(moi != currentIndex)
    9. {
    10. PodModel *mymodel = const_cast<PodModel*>(dynamic_cast<const PodModel*>(index.model()));
    11. currentIndex = moi;
    12. mymodel->wantsUpdate();
    13. }
    14. }
    15. .....
    16. }
    17. QSize DirObjectDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index ) const
    18. {
    19. QSize s;
    20. s = QSize(option.decorationSize.width(), option.decorationSize.height());
    21. if(currentIndex == index.row())
    22. {
    23. s.setHeight(option.decorationSize.height()*2);
    24. }
    25. return s;
    26. }
    To copy to clipboard, switch view to plain text mode 
    If you have questions to the code PM me!
    Good luck!
    Attached Files Attached Files

  4. The following 2 users say thank you to kernel_panic for this useful post:

    Lykurg (26th February 2009), matthieu (23rd September 2012)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [solved]QListView dynamic item height

    Great! Thanks, had a short look and it worked fine.
    I'll try tomorrow to get it transformed into my needs... (A simple standard QListWidget with custom non editable items, which have different heights when selected of course...)

    Once again thank you very much.

    Lykurg

  6. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [solved]QListView dynamic item height

    Hi Lykurg...
    Did you find any solution ? I was also trying similar thing with QListWidget... but there are many issues,,, Qt doesnt split text if space is not there.
    Even if you manage to explicitly insert a space, the are is clipped to option.rect . Again if you dont clip it, all text will be displayed, but the extra area wont be updated.

    if your problems are same, may be we can find a solution. I have so far limited my display lines to 2 rows... atleast I get to see extra text than in one line.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [solved]QListView dynamic item height

    Hi,

    unfortunately I currently don't have not much time (because I am abroad and I am about to learn a new language without any knowledge of it...(and in a country where the people neither speak my language nor English, it isn't fun!)), so my "scientific" interest are cut down.

    For the issue with the item height, I currently go the same way as kernel_panic with a model and delegate. But I am still not done. Problems are:
    - definitely calculate the new item height when selected, which varies for each item. (Here's the problem with the text. I guess QFontMetrics must help.)
    - reduce repainting (Because this time every selection change causes an repaint of all items, not only the visible)

    So long, if I see clearer I will post. Maybe it would be good if we open a wiki page for that.


    Lykurg

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [solved]QListView dynamic item height

    With model and delegate it can me done as kernel panic suggested...
    but with listwidget you cant have your own model
    Also i was searching way which wud merge with Qt's flow...like all decoration position and icon sizes are taken into consideration.
    Even with kernel panics code, text is not displayed in icon mode, thought the wantsUpdate trick can still be used.
    But I dont want to do ALL the drawing myself,,, just specify rects properly and let Qt do the rest

    neways njoy ur trip,,, when u come back u can reply back

  9. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [solved]QListView dynamic item height

    Hi,

    I have played with the code and added to my view an "QSortFilterProxyModel". so I have changed the paint method as follow:
    Qt Code:
    1. QSortFilterProxyModel *proxymodel = const_cast<QSortFilterProxyModel*> (dynamic_cast<const QSortFilterProxyModel*> (index.model()));
    2. PodModel *mymodel = const_cast<PodModel*> (dynamic_cast<const PodModel*> (proxymodel ? proxymodel->sourceModel() : index.model()));
    To copy to clipboard, switch view to plain text mode 
    but when calling mymodel->wantsUpdate(); the application crashes:
    Thread [1] (Suspended: Signal 'SIGSEGV' received. Description: Segmentation fault.)
    36 QSortFilterProxyModel :: parent() /qt/qtsdk-2009.01/qt/src/corelib/kernel/qabstractitemmodel.h:65 0xb6ba3372
    35 QListView :: rectForIndex() /qt/qtsdk-2009.01/qt/src/corelib/kernel/qabstractitemmodel.h:369 0xb6b40db0
    34 QListView :: visualRect() /qt/qtsdk-2009.01/qt/src/gui/itemviews/qlistview.cpp:576 0xb6b43a4a
    33 QListView :: paintEvent() /qt/qtsdk-2009.01/qt/src/gui/itemviews/qlistview.cpp:1128 0xb6b3dff5
    32 QWidget :: event() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:7654 0xb65e061a
    31 QFrame :: event() /qt/qtsdk-2009.01/qt/src/gui/widgets/qframe.cpp:559 0xb69da4f3
    30 QAbstractScrollArea :: viewportEvent() /qt/qtsdk-2009.01/qt/src/gui/widgets/qabstractscrollarea.cpp:962 0xb6a75f3c
    29 QAbstractItemView :: viewportEvent() /qt/qtsdk-2009.01/qt/src/gui/itemviews/qabstractitemview.cpp:1466 0xb6b226d3
    28 QAbstractScrollAreaFilter :: eventFilter() /qt/qtsdk-2009.01/qt/src/gui/widgets/qabstractscrollarea_p.h:100 0xb6a77ec5
    27 QCoreApplicationPrivate :: sendThroughObjectEventFilters() /qt/qtsdk-2009.01/qt/src/corelib/kernel/qcoreapplication.cpp:718 0xb6008d09
    26 QApplicationPrivate :: notify_helper() /qt/qtsdk-2009.01/qt/src/gui/kernel/qapplication.cpp:4080 0xb6584a59
    25 QApplication :: notify() /qt/qtsdk-2009.01/qt/src/gui/kernel/qapplication.cpp:4049 0xb65887be
    24 QCoreApplication :: notifyInternal() /qt/qtsdk-2009.01/qt/src/corelib/kernel/qcoreapplication.cpp:602 0xb600881b
    23 QWidgetPrivate :: drawWidget() /qt/qtsdk-2009.01/qt/src/corelib/kernel/qcoreapplication.h:216 0xb65d7d82
    22 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5156 0xb65d8549
    21 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5147 0xb65d8444
    20 QWidgetPrivate :: drawWidget() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5095 0xb65d790a
    19 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5156 0xb65d8549
    18 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5147 0xb65d8444
    17 QWidgetPrivate :: drawWidget() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5095 0xb65d790a
    16 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5156 0xb65d8549
    15 QWidgetPrivate :: drawWidget() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5095 0xb65d790a
    14 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5156 0xb65d8549
    13 QWidgetPrivate :: drawWidget() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5095 0xb65d790a
    12 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5156 0xb65d8549
    11 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5147 0xb65d8444
    10 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5147 0xb65d8444
    9 QWidgetPrivate :: paintSiblingsRecursive() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5147 0xb65d8444
    8 QWidgetPrivate :: drawWidget() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:5095 0xb65d790a
    7 QWidgetBackingStore :: sync() /qt/qtsdk-2009.01/qt/src/gui/painting/qbackingstore.cpp:1266 0xb67ac0b7
    6 QWidgetPrivate :: syncBackingStore() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget.cpp:1603 0xb65d02b5
    5 QETWidget :: translateConfigEvent() /qt/qtsdk-2009.01/qt/src/gui/kernel/qapplication_x11.cpp:5291 0xb65e8c14
    4 QApplication :: x11ProcessEvent() /qt/qtsdk-2009.01/qt/src/gui/kernel/qapplication_x11.cpp:3448 0xb65f5db8
    3 qt_x11_wait_for_window_manager() /qt/qtsdk-2009.01/qt/src/gui/kernel/qwidget_x11.cpp:356 0xb660f162
    2 QSplashScreen :: finish() /qt/qtsdk-2009.01/qt/src/gui/widgets/qsplashscreen.cpp:226 0xb6a41d71
    1 main() /home/lykurg/workspace/RMEingabe/src/main.cpp:43 0x0806a890
    Why, I dont understand it?

    Thanks Lykurg

    EDIT: it's in "inline QModelIndex::QModelIndex(const QModelIndex &other) : r(other.r), c(other.c), p(other.p), m(other.m) {}"

  10. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [solved]QListView dynamic item height

    Damn, the solution comes always after posting

    Qt Code:
    1. currentIndex = moi;
    2. SortFilterBibtexModel *proxymodel = const_cast<SortFilterBibtexModel*> (dynamic_cast<const SortFilterBibtexModel*> (index.model()));
    3. if (proxymodel)
    4. proxymodel->wantsUpdate();
    5. else
    6. {
    7. BibtexModel *mymodel = const_cast<BibtexModel*> (dynamic_cast<const BibtexModel*> (proxymodel ? proxymodel->sourceModel() : index.model()));
    8. mymodel->wantsUpdate();
    9. }
    To copy to clipboard, switch view to plain text mode 

    where BibtexModel is a subclass of QAbstractListModel and SortFilterBibtexModel of QSortFilterProxyModel. SortFilterBibtexModel has a
    Qt Code:
    1. void wantsUpdate()
    2. {
    3. emit layoutChanged();
    4. }
    To copy to clipboard, switch view to plain text mode 

    So long...

Similar Threads

  1. How to Modify QListView item height?
    By blackfox in forum Qt Programming
    Replies: 7
    Last Post: 16th May 2012, 12:38
  2. QListView + row height
    By NoRulez in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2012, 09:53
  3. find item in QListView
    By AD in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 14:50
  4. QListView item alignment
    By innerhippy in forum Qt Programming
    Replies: 2
    Last Post: 11th November 2008, 10:32
  5. how to move item up and down in QListView
    By zhanglr in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2008, 15:39

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.