Results 1 to 6 of 6

Thread: Using Designer to implement a QItemDelegate

  1. #1
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Using Designer to implement a QItemDelegate

    Hi All,

    I'm trying to use a QListWidget to draw Items. Because the items are complex, I also create a CustomListWidgetItem and override the operator< to order them.

    The problems consist when I try to draw the item. The first aproach consist in create a widget and associate them to the Item with QListWidget::setItemWidget from the list, but I think it is not correct. After looking in the forum I found out that could be better to implement a CustomItemDelegate and override paint to draw what I need.

    Is there any way to use the designer output to avoid draw all manually?

    Is my approach correct?

    Thanks in advance

  2. #2
    Join Date
    Jul 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: Using Designer to implement a QItemDelegate

    the main question: what do you want to draw? if your items can be drawn just displaing Qt:isplayRole data, then you dont need to create Delegate.

  3. #3
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Using Designer to implement a QItemDelegate

    Sorry, I'm currently try to understand what you meand. In addition I'll try to show what I'm trying to do.
    My idea (I don't know if it is the best) is to use the render method of the widget prom the delegate:aint
    Becuse the widget has to show 3 labels and an image in principle, and in the future could be change and I think that if I use the designer and someting change, I won't have to change the code.

    The problem is that when I try to render the widget, I can't indicate where it should be draw
    This is an example:

    Qt Code:
    1. void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    2. {
    3. ItemWidget item;
    4.  
    5. item.setGeometry(option.rect);
    6.  
    7. item.render(painter);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Regards

  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: Using Designer to implement a QItemDelegate

    I guess you will need to calculate that yourself.
    Using a ItemWidget inside paint is quite heavy. You are literally creating and destroying a widget on every paint event !!

    Proper way would be, calculate the rects of text and image you want to draw and simply draw them. dont use any widget.render() .
    If more curious, you can dig into code of QItemDelegate and see how it renders the item. You will get the idea.

  5. #5
    Join Date
    Jul 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: Using Designer to implement a QItemDelegate

    look at my example:

    Qt Code:
    1. QLabel renderer;
    2. renderer.setObjectName("renderer");
    3. renderer.setStyleSheet(getStyleSheet(index));
    4. renderer.setText(makeDisplayString(index));
    5. renderer.resize(option.rect.size());
    6. painter->save();
    7. painter->translate(option.rect.topLeft());
    8. renderer.render(painter);
    9. painter->restore();
    To copy to clipboard, switch view to plain text mode 

    here i render label with my style sheet.

    you must draw here:

    Qt Code:
    1. option.rect.topLeft()
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2010
    Posts
    20
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Using Designer to implement a QItemDelegate

    And what about creating only one widget which is a member of the delegate to avoid the creation of the object.

    And still using the render, or is any other thing to take care about that?

    Rigth now, the item is not so complicated I'll try to make it by hand.

Similar Threads

  1. How does Qt implement layers?
    By FinderCheng in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2009, 13:12
  2. how to implement this widget
    By pencilren in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2007, 11:10
  3. What's the best way to implement a MainWindow
    By darkadept in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2007, 17:46
  4. how to implement it ?
    By qtopiahooo in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2006, 17:01
  5. Replies: 3
    Last Post: 12th May 2006, 19:31

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.