Results 1 to 2 of 2

Thread: delegate paint not draw QTreeWidget selected items correctly

  1. #1
    Join Date
    Aug 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default delegate paint not draw QTreeWidget selected items correctly

    Hello,

    how can I draw each item in a QTreeView - also the column 0 should not have a "selected" colored background.
    The selected color is at the moment "blue", but only column 1 should be "blue" or other QColor.

    Here is the code - thanks for helping

    Qt Code:
    1. class NoEditDelegate: public QStyledItemDelegate
    2. {
    3. public:
    4. NoEditDelegate(QObject* parent=0): QStyledItemDelegate(parent)
    5. {
    6. }
    7. virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    8. {
    9. return 0;
    10. }
    11. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    12. {
    13. if (option.state == QStyle::State_Selected
    14. || option.state == QStyle::State_Active
    15. || option.state == QStyle::State_Enabled
    16. || option.state == QStyle::State_Item
    17. || option.state == QStyle::State_HasFocus)
    18. {
    19. QStyleOptionViewItem newOption(option);
    20. newOption.palette.setBrush(QPalette::Highlight,QColor(200,100,200));
    21. QStyledItemDelegate::paint(painter, newOption, index);
    22. return;
    23. }
    24. QStyledItemDelegate::paint(painter, option, index);
    25. }
    26. };
    27.  
    28. void MyTreeWidget::set_items(QWidget *p)
    29. {
    30. QTreeWidgetItem *__qtreewidgetitem = new QTreeWidgetItem(this);
    31. QTreeWidgetItem *__qtreewidgetitem1 = new QTreeWidgetItem(__qtreewidgetitem);
    32. __qtreewidgetitem1->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
    33.  
    34. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    35. setRootIsDecorated(true);
    36. setAllColumnsShowFocus(false);
    37. header()->setDefaultSectionSize(100);
    38.  
    39. QTreeWidgetItem *___qtreewidgetitem = headerItem();
    40. ___qtreewidgetitem->setText(1, "Wert");
    41. ___qtreewidgetitem->setText(0, "Eigenschaft");
    42.  
    43.  
    44. // here
    45. NoEditDelegate *ne = new NoEditDelegate(this);
    46.  
    47. setSortingEnabled(false);
    48. setItemDelegateForColumn(0, ne); // <--- here
    49.  
    50. QTreeWidgetItem *___qtreewidgetitem1 = topLevelItem(0);
    51. ___qtreewidgetitem1->setText(0, "Anzeige");
    52.  
    53.  
    54. QTreeWidgetItem *___qtreewidgetitem2 = ___qtreewidgetitem1->child(0);
    55. ___qtreewidgetitem2->setText(1, "1");
    56. ___qtreewidgetitem2->setText(0, "pageno");
    57.  
    58. QTreeWidgetItem *___qtreewidgetitem3 = new QTreeWidgetItem(this);
    59. insertTopLevelItem(0,___qtreewidgetitem3);
    60. QTreeWidgetItem *___qtreewidgetitem4 = topLevelItem(1);
    61.  
    62. ___qtreewidgetitem4->setText(0, "Bearbeiten");
    63. }
    64.  
    65. void MyTreeWidget::paintEvent(QPaintEvent *event)
    66. {
    67. QTreeWidget::paintEvent(event);
    68.  
    69. int pHeight = font().pointSize();
    70. int pWidth = columnWidth(0);
    71.  
    72. p.begin(viewport());
    73.  
    74. int rc = 10;
    75. int py = 0;
    76.  
    77. for(int i = 0; i < rc; i++)
    78. {
    79. p.drawRect(1,py,pWidth-2,pHeight+5);
    80. p.drawRect(columnWidth(0)-1,py,columnWidth(1),pHeight+5);
    81. py += pHeight+5;
    82. }
    83.  
    84. p.end();
    85. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: delegate paint not draw QTreeWidget selected items correctly

    Did you try this?

    Qt Code:
    1. QTreeWidget::setSelectionBehavior(QAbstractItemView::SelectItems);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Delegate paint event bug?
    By jerry7 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2010, 03:14
  2. Replies: 1
    Last Post: 9th September 2010, 19:26
  3. Replies: 0
    Last Post: 30th August 2010, 20:08
  4. Need help figuring out how to paint graphicsRectItems correctly
    By lightning2911 in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2010, 08:39
  5. Use delegate to draw different type of items
    By nifei in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2009, 14:16

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.