Results 1 to 20 of 37

Thread: [Qt4] Noob and custom Item Delegate

Threaded 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

    Question [Qt4] Noob and custom Item Delegate

    Hello! I'm trying to create custom item delegate to view multiline text in QTreeView properly... So I've started trying to create my delegate... the result is that it doesn't draw anything (the scrollbar appear when i resize app window so items are there) :| Here is some code:

    painting:
    Qt Code:
    1. void rosterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. painter->setRenderHint(QPainter::TextAntialiasing);
    4. painter->setPen(Qt::NoPen);
    5.  
    6. if (option.state & QStyle::State_Selected)
    7. painter->setBrush(option.palette.highlight());
    8. else
    9. painter->setBrush(QBrush(Qt::white));
    10.  
    11. QTextOption textOption;
    12. textOption.setWrapMode(QTextOption::WordWrap);
    13.  
    14. QSize sizehint=sizeHint(option, index);
    15. int width=Qxygen->viewWidth();
    16. int height=ceil((sizehint.width()*sizehint.height())/width);
    17.  
    18. rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
    19.  
    20. painter->drawText(QRectF(option.rect.x(), option.rect.y(), width, height), /* FROM WHERE GET THAT TEXT? */, textOption);
    21. }
    To copy to clipboard, switch view to plain text mode 

    here is my modelView data
    Qt Code:
    1. QVariant rosterView::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. if (role == Qt::DisplayRole)
    7. {
    8. rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
    9.  
    10. if(descr && !(item->isGroup())) // CHECKS IF VIEW HAS TO SHOW ITEM DESCRIPTION AND ITEM ISN'T GROUP - GROUPS DOESN'T NEED DESCRIPTION
    11. {
    12. if((item->data(1)).isEmpty()) return item->data(index.column());
    13. else return QString(item->data(index.column()))+"\n"+QString(item->data(1));
    14. }
    15. else
    16. {
    17. return item->data(index.column());
    18. }
    19. }
    20. else if(role == Qt::DecorationRole)
    21. {
    22. rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
    23. if(item->isGroup())
    24. {
    25. if(item->isExpanded())
    26. {
    27. return QIcon(":expanded.png");
    28. }
    29. else
    30. {
    31. return QIcon(":collapsed.png");
    32. }
    33. }
    34. else
    35. {
    36. return QIcon(":dnd.png");
    37. }
    38. }
    39. else
    40. {
    41. return QVariant();
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    Now i have no idea how to make my delegate paint icon and text (in one column). Any suggestion/help?
    Last edited by naresh; 13th March 2006 at 11:36.

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.