Results 1 to 7 of 7

Thread: Need help with my delegate

  1. #1
    Join Date
    Dec 2009
    Posts
    41
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Need help with my delegate

    Hi !

    I am using a custom delegate to display my qlistview. Indeed, I want each of the items to be written on two lines and using html syntax to highlight it. No need for editing the items so I have just reimplemented the painter method :

    Qt Code:
    1. void AlertDelegate::paint (QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const {
    2.  
    3. painter->save();
    4. doc.setHtml( index.data().toString() );
    5. QAbstractTextDocumentLayout::PaintContext context;
    6. doc.setPageSize(option.rect.size());
    7. painter->translate(option.rect.x(), option.rect.y());
    8. doc.documentLayout()->draw(painter, context);
    9. painter->restore();
    10. }
    To copy to clipboard, switch view to plain text mode 

    But the problem is that the first line of the second item is written on the second line of the first item. Where is the problem ?


    Thanks

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

    Default Re: Need help with my delegate

    You should also implement sizeHint() to let the view know how big each item is.
    I guess in your case, you could just double the height (more or less).

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

    Guilo (1st July 2010)

  4. #3
    Join Date
    Dec 2009
    Posts
    41
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with my delegate

    Is sizeHint a method of QItemDelegate or an attribute of QItemDelegate ? Or an attribute of my QTextDocument ? Any example somewhere to implement this ? cause I don't know how to double the actual value.

    Thanks

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

    Default Re: Need help with my delegate

    sizeHint() is a function of a widget which returns how big the widget is.

    Example:
    Qt Code:
    1. QSize AlertDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. // Do some checks with the text document so you know the returned value is correct. If you use a pointer for example, check if it exists. etc...
    4.  
    5. return myDoubleLineTextDocument.sizeHint();
    6. }
    To copy to clipboard, switch view to plain text mode 

    You will need to define your textdocument more accessible in your delegate.

  6. #5
    Join Date
    Dec 2009
    Posts
    41
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with my delegate

    QTextDocument has no member sizeHint()cause it inherits QObject and not QWidget. And what about painter and context ?

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

    Default Re: Need help with my delegate

    It has a size() function.

    Or whatever else that you can use to get the size of the text.

  8. #7
    Join Date
    Dec 2009
    Posts
    41
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with my delegate

    It works quite great. But each time I update my model (with clear() and appendRow()), some space must appear on the right side of the text beacause the horizontal scrollbar gets smaller each update.

Similar Threads

  1. Is delegate necessary here?
    By scythe in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 19:59
  2. QTreeView Delegate
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2009, 09:10
  3. QTableWidget Delegate
    By aekilic in forum Qt Programming
    Replies: 12
    Last Post: 15th May 2009, 09:21
  4. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  5. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 20:28

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.