Results 1 to 5 of 5

Thread: Paint in Custom Delegate

  1. #1
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Paint in Custom Delegate

    I'm currently looking to display the text of the first column in a treeview in such way that the text takes up the space of both column one and two - sort of like a merged cell in MS Excel. The approach must only apply for item in the tree that have children branches underneath.

    I've subclass the QItemDelegate in the following way

    Qt Code:
    1. class PostDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PostDelegate(QObject *parent=0) : QItemDelegate(parent){}
    7. ~PostDelegate(){}
    8. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    9. {
    10. if(index.model()->hasChildren(index) && index.column() < 2 ) { //GroupProxyRecord
    11. QItemDelegate::drawBackground(painter , option , index) ; //draw bagground first
    12. QModelIndex idx = index;
    13. if(index.column() == 1) { //redirect to column 0
    14. idx = index.sibling(index.row() , 0) ;
    15. }
    16. QSize s = sizeHint(opt , idx) ;
    17. QString value = idx.data().toString() ;
    18. QRectF rectF = painter->boundingRect ( opt.rect , Qt::AlignLeft, value ) ;
    19. painter->drawText(rectF, value );
    20.  
    21. } else {
    22. QItemDelegate::paint(painter , option , index) ;
    23. }
    24.  
    25. }
    26.  
    27. QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
    28. {
    29. return QItemDelegate::sizeHint(option, index);
    30. }
    31.  
    32. };
    To copy to clipboard, switch view to plain text mode 

    Now, I'm almost there but I can't seem to calculate the rectangle that covers index column one and two in order to calculate the right QRect for the painter.

    If you look at the attached picture the merge cell paint must only apply to row 0,1,5 and 9.

    My question is: How can I retrieve/calculate the rect associated with a given QModelIndex?

    PostDelegate2.png

    Any suggestions out there?

  2. #2
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint in Custom Delegate

    What's going on guys? No Delegate / Paint experts out there ?

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

    Default Re: Paint in Custom Delegate

    If it is just about getting the size then you can ask the horizontal header for the width of an appropriate section. But honestly I don't think that what you are doing is a good approach. I would rather lean towards reimplementing QTreeView::drawRow() or QAbstractItemView::visualRect() to physically make the item span across two columns instead of emulating such a look.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint in Custom Delegate

    Hi wysota, thank you for your reply.
    The reason I'm subclassing the QItemDelegate is that it's far simpler to subclass than the QTreeView . The second reason is that the QItemDelegate must work on both treeviews and tableview without having to subclass every time. Plus I may loose or I need to consider built in functionality in the treeview / tableview like the alternating row color. In that respect it's far easier to subclass the QItemDelegate.

    What I've started to do is the following which I think is kind of what you're suggesting, right?;
    Qt Code:
    1. QAbstractItemView *view = qobject_cast<QAbstractItemView *>(parent() );
    2. if(!view) return;
    3. QRect rec = view->visualRect(idx);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Paint in Custom Delegate

    Quote Originally Posted by Nightfox View Post
    In that respect it's far easier to subclass the QItemDelegate.
    It's also far easier to break the design. I.e. if someone reorders columns in the table view.

    If you reimplement visualRect() or drawRow() you will not lose alternating rows. It's more work if you want to implement drawRow() without losing what it is doing though. With visualRect() you should be safe.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Delegate doesn't paint properly
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2010, 11:43
  2. QListWidget delegate paint problem.
    By rule in forum Qt Programming
    Replies: 0
    Last Post: 5th July 2010, 13:31
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. Delegate paint optimisation
    By bunjee in forum Qt Programming
    Replies: 11
    Last Post: 21st April 2008, 19:48

Tags for this Thread

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.