1

I have reimplemented paint() function for QTreeWidget, I want to show data of second column bold, but it doesn't work.

How can i fix it?

void extendedQItemDelegate::paint(QPainter *painter,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    const QString txt = index.data().toString();
    painter->save();
    QFont painterFont;
    if (index.column() == 1) {
        painterFont.setBold(true);
        painterFont.setStretch(20);
    }
    painter->setFont(painterFont);
    drawDisplay(painter, option, rect, txt);
    painter->restore();
}

I attached a screen shot of the problem, second half should be bold

screen shot

ymoreau
  • 3,402
  • 1
  • 22
  • 60
mari
  • 417
  • 1
  • 6
  • 21
  • Please always explain in general how it does not work. It is perhaps even better to provide screenshots if it is a visual issue. – László Papp Mar 15 '14 at 09:00

2 Answers2

0

You forgot to add your extendedQItemDelegate to the QTreeView/QTreeWidget object via the setItemDelegate member function.

As an example:

QTreeWidget* tree_view = ...;
extendedQItemDelegate* extended_item_delegate = ...;
tree_view->setItemDelegate(extended_item_delegate);
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • here is my code :extendedQItemDelegate *delg = new extendedQItemDelegate; treeEvents->setItemDelegate(delg); – mari Mar 15 '14 at 11:46
  • i googled my probelm and i found that i should change const QStyleOptionViewItem &option font but because it is const i can't change it, i try to change a copy of it but it shows nothing – mari Mar 15 '14 at 11:49
0

You need to make a copy of the const QStyleOptionViewItem &option, apply your font changes to that copy, then paint using your copy instead of the original option passed to the function.

void extendedQItemDelegate::paint(QPainter *painter,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    const QString txt = index.data().toString();
    painter->save();
    QStyleOptionViewItem optCopy = option;     // make a copy to modify
    if (index.column() == 1) {
        optCopy.font.setBold(true);            // set attributes on the copy
        optCopy.font.setStretch(20);
    }
    drawDisplay(painter, optCopy, rect, txt);  // use copy to paint with
    painter->restore();
}

(Just realized this is an old question but it had popped up to the top of qt tags.)

Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22