Results 1 to 9 of 9

Thread: Different list widget item sizes in one QListWidget - How to?

  1. #1
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    3

    Default Different list widget item sizes in one QListWidget - How to?

    Hi,

    I'm a complete Qt newbie. I'm creating a GUI that requires a QListWidget with list widget items that have different sizes aside from it's customized look. I have subclassed QAbstractItemDelegate and customized the drawing of each list item. But I can't find a way to make the last item smaller than the rest of the items, in terms of height.

    Is this because the reimplimented sizeHint function from the base class QAbstractItemDelegate can only be set once? That is why all list items that is bound to this delegate will ahve the same size hint? I'm looking for ways to change the sizeHint inside QAbstractItemDelegate paint function for the smaller list item. But I don't know if this is the right way.

    Can somebody share your ideas about how to make different sizes for a list item that are bound to the same delegate?

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

    Default Re: Different list widget item sizes in one QListWidget - How to?

    Can you share how you have implemented the sizeHint function ?
    May be you are not returning values based on index.

    Also it will be easier if you use QItemDelegaterather than QAbstractItemDelegate

  3. #3
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Different list widget item sizes in one QListWidget - How to?

    aamer4yu,

    Can you share how you have implemented the sizeHint function ?
    First of all, my sizeHint function simply returns a desired width and height.

    Qt Code:
    1. QSize QCustomListWidgetItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. return QSize(customWidth, customHeight);
    4. }
    To copy to clipboard, switch view to plain text mode 

    In my QCustomListWidgetItemDelegate:aint function I'm drawing each item. All visible items in the QListWidget except for the first and last items have dimensions customWidth and customHeight. I would like to draw the first and last visible items with smaller dimension particularly the height.

    Also it will be easier if you use QItemDelegate rather than QAbstractItemDelegate
    I am not familiar of the advantage of using QItemDelegate over QAbstractItemDelegate, please explain if have time. Thanks.!

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

    Default Re: Different list widget item sizes in one QListWidget - How to?

    I am not familiar of the advantage of using QItemDelegate over QAbstractItemDelegate, please explain if have time.
    QItemDelegate is derived from QAbstractItemDelegate and provides more easy implementation of functions like drawCheck, drawDisplay, drawFocus, etc.

    You are simply returning ONE size for all items.. you will need to check the row and return size based on it.
    ie. if(row == 0 || row==last) return smaller size..

    You can get the row from QModelIndex::row To know if the row is last, you will need info about the listwidget.
    You can maintain a pointer to listwidget in the delegate class..
    then in sizeHint function check -
    Qt Code:
    1. if(row ==0 || row == pListWidget->count() -1)
    2. return customSize;
    3. else
    4. return QItemDelegate::sizeHint(..);
    To copy to clipboard, switch view to plain text mode 

    Hope you get the idea

  5. #5
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Different list widget item sizes in one QListWidget - How to?

    Thanks aamer4u! Your suggestion helped me. But I still have a problem. I'm sorry I was not very clear about my statement,
    . I would like to draw the first and last visible items with smaller dimension particularly the height
    What I exactly mean is that I need tor draw the "first visible" and "last visible" items in the list with smaller dimensions. I already have a function to determine the first visible and last visible items (thanks to wysota), using the QListWidget function "indexAt". But when I tried calling these functions inside my custom QAbstractItemDelegate::sizeHint it always throws me a segmentation fault. Let me illustrate below,

    The ff. is how I get the first visible item (similar with the last visible item), these code are in my custom QAbstractItemDelegate::sizeHint function.
    Qt Code:
    1. QListWidget * parent = dynamic_cast<QListWidget*>(this->parent());
    2. if(parent)
    3. {
    4. QModelIndex firstIndex = QCustomUtils::getInstance()->firstVisibleItem(parent); ---> this caused a segmentation fault some time, sometimes its ok
    5. if(index.row() == firstIndex.row())
    6. {
    7. reduce size of first visible item
    8. }
    9. // do similar with last visible item
    10.  
    11. // return custom size hint
    To copy to clipboard, switch view to plain text mode 

    What could be the reason for the segmentation fault? The "first visible" and "last visible" items in the list are dynamic, they can be any of the rows. So checking just the row would not be sufficient. Do you have any other ideas about this, or how should I do this?

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

    Default Re: Different list widget item sizes in one QListWidget - How to?

    You can use QAbstractItemView::visualRect to determine the rect of the item and then check if this rect lies within the top or bottom of the rect of the listwidget.
    That way I guess you can determine if the item is at top or bottom of the list widget

  7. The following user says thank you to aamer4yu for this useful post:

    slscripters (21st May 2010)

  8. #7
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Different list widget item sizes in one QListWidget - How to?

    aamer4yu, looks like this is a better idea than using indexAt. However, I still got the segmentatoin fault error even when using visualRect. I'm running out of ideas and ways to debug this, maybe I'm doing something wrong here. I've even tried just calling the ff. code, inside the sizeHint function just for a test, but still it gives a segmentation fault error after the last item is added in the parent list.

    Qt Code:
    1. parent->visualRect(index)
    To copy to clipboard, switch view to plain text mode 

    Again, this code is in my custom QAbstractItemDelegate::sizeHint function. Why is it raising a segmentation fault error at some time while its working for the first addition of the items?

    Are there any restrictions inside sizeHint function? Is this thread safe? I'm running out of ideas...please help.

  9. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Different list widget item sizes in one QListWidget - How to?

    Check if parent exists.

  10. The following user says thank you to tbscope for this useful post:

    slscripters (21st May 2010)

  11. #9
    Join Date
    Apr 2010
    Location
    Bahamas
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Different list widget item sizes in one QListWidget - How to?


    Check if parent exists.
    I already have this check but still segmentation fault exists (weird). I debug using qDebug as of now, I don't have time for using any visual debugger right now. My Qt Creator does not work for my embedded setup. So I didn't know what caused the seg fault. Anyway thanks to everybody especially to aamer4u, he has lead me to other solution. What I did is I tagged each item with UserRoles and retrieved them when drawing in the QAbstractItemDelegate:aint function, and draw accordngly for first and last visible item. This solution may not be the best, but it worked and it is straightforward. Thanks!!!

Similar Threads

  1. Replies: 8
    Last Post: 1st October 2015, 07:23
  2. Replies: 0
    Last Post: 28th April 2010, 11:41
  3. Replies: 3
    Last Post: 25th February 2010, 18:52
  4. How to use a button/widget as item for QListWidget?
    By youkai in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 15:34
  5. Replies: 2
    Last Post: 20th April 2007, 15:36

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
  •  
Qt is a trademark of The Qt Company.