Results 1 to 20 of 37

Thread: [Qt4] Noob and custom Item Delegate

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    fmetrics.height() is string height and fmetrics.width(...) is string width lines in qDebug output

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Qt4] Noob and custom Item Delegate

    Looks like options.rect is incorrect. Do you use a standard view? What model do you use?
    Last edited by wysota; 14th March 2006 at 20:04.

  3. #3
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    I'm using standard QTreeView. I'm using my custom model based on QAbstractItemModel, and no i didn't reimplement visualRect in it. And here is new sizeHint... it seems to calculate better but painting is worse :|

    Qt Code:
    1. QSize rosterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QRect tmp=option.fontMetrics.boundingRect(option.rect.x(), option.rect.y(), option.rect.width(), 1000, Qt::AlignLeft|Qt::AlignTop|Qt::TextWordWrap, index.model()->data(index, DescriptionRole).toString());
    4. QImage img=index.model()->data(index, Qt::DecorationRole).value<QImage>();
    5. int height;
    6.  
    7. if(tmp.height())
    8. {
    9. height=img.height()+tmp.height()+2;
    10. }
    11. else
    12. {
    13. height=img.height()+2;
    14. }
    15.  
    16. qDebug("%d %d", option.rect.width(), height);
    17.  
    18. return QSize(option.rect.width(), height);
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Qt4] Noob and custom Item Delegate

    Could you explain "better" and "worse"?

  5. #5
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    Well better calculating means that size is now as i need (as i can see after calling painter->drawRect() ) and painting. And the worse is seen on screen. I dont know why but image isn't painted anymore (well ill fix it somehow) but this huge gap :| I dont know where is it from :|

    I'm putting sources in attachment. Could you take a look at them?
    Attached Images Attached Images
    Last edited by naresh; 15th March 2006 at 19:12.

  6. #6
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    I had to remove source code, here is new one (fixed a bit btw). Could you take a look at it?
    Attached Files Attached Files

  7. #7
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    Ok i have an idea how to calculate y position for all items... but only thing I need is to pass QTreeView::verticalOffset() (whitch is virtual protected)... best way I see is passing it with QStyleOptionViewItem (to QItemDelegate::paint(...)). Any idea how to do that?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Qt4] Noob and custom Item Delegate

    Ok, I think I know what wrong. Looks like the view doesn't ask for the sizeHint each time it is resized, but only when an item changes. So you have to force the model to emit layoutChanged() on every view resize. Unfortunately you can't do it in the delegate, you should probably install an event filter on the view and provide a wrapper method for the model, which will emit layoutChanged() whenever the width of the view changes. It should be enough for your items to resize properly.

  9. #9
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    I checked throu whole painting (from view to delegate) and it paints wrong becouse of QTreeView:aintEvent(...). It calculates y position of each item depending on verticalStepsPerItem(). It can't be never calculated well becouse items have different heights... I've reimplemented this method but atm i have one problem... verticalScrollBar can move slider under all items. So now i don't know how to control verticalScrollBar. I've tried hiding it when viewport height is >= sizeHint height but there always left gray space for scroll bar. here is some code. Changing maximum for scrollBar causes scrollbar appear and disappear and so on... Also this painting seems to be really slow... Any ideas how to make it faster and control scrollbar? I'm placing source code to see what i have already done (and what am i missing)

    EDIT: Another strange thing... It seems that items with descr have bigger range, i mean tooltip for those items is shown even if i place cursor on item under it :|
    Attached Files Attached Files
    Last edited by naresh; 17th March 2006 at 18:22.

  10. #10
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Qt4] Noob and custom Item Delegate

    At least, after about a week of work it works like it should. First of all i've read whole thread few times to get it how was it painted before. It was taking width stright from viewport not from QStyleOptionViewItem. So i've added int width into delegate and method setWidth(int). I've reimplemented only resizeEvent for my rosterWidget class calling QTreeView::resizeEvent after setting width for delegate, and emiting layoutChanged for model (by my method emitLayoutChanged()). Delegate calculates sizeHint depending on width. I also added after every collaspe/expand item setting width (becouse scrollbar may appear and the viewport may resize without resizing widget). Now it paints and update well... As i expected. Thank you wysota for all your precious tips. Without them i would never make it.

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.