Results 1 to 5 of 5

Thread: correct sizeHint of QItemDelegate

  1. #1
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question correct sizeHint of QItemDelegate

    I subclassed QItemDelegate to create some custom/standard labels in the view and open them persistent. the labels' size changes from time to time, depending on the model's data. i wonder how to return a correct sizeHint so that the view knows what size should be assigned to each item. i did like this, obviously, it is not a good way to calculate the right sizeHint, and it doesn't return right size when the data changes.

    QSize LabelDelegate::sizeHint(const QStyleOptionViewItem &option,
    const QModelIndex &index)const
    {
    UnselectedLabel *label = new UnselectedLabel(index.data().toString());
    QString str = index.data().toString();
    label->adjustSize();
    QSize sizeHint = label->size();
    return sizeHint;
    }

    Any help will be appreciated, thank you.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: correct sizeHint of QItemDelegate

    try to reimplement QItemDelegate::updateEditorGeometry in such way
    Qt Code:
    1. void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
    2. {
    3. editor->setGeometry(option.rect);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: correct sizeHint of QItemDelegate

    Quote Originally Posted by spirit View Post
    try to reimplement QItemDelegate::updateEditorGeometry in such way
    Qt Code:
    1. void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
    2. {
    3. editor->setGeometry(option.rect);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Thank you. i did just as you said.However, it seems that the updataEditorGeometry cannot change the item's position, it just change the widget's position and size. the items are still not high enough. once the widgets' sizes change, they are placed the same position, that just look like this: the 2ed picture shows the overlapping labels, since the 1st label's height increased and the delegate didnot change the size of an item.
    Attached Images Attached Images

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: correct sizeHint of QItemDelegate

    you can change width and height of item using QHeaderView::defaultSectionSize
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    nifei (17th November 2008)

  6. #5
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: correct sizeHint of QItemDelegate

    Thank you. i'll try.

    but my view is supposed to hide it's horizontal & vertical headers.

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.