views:

219

answers:

1

By default a QTreeWidget manages the selection of rows [when you click a row it highlights it, when you click another row it highlights that and deselects the previous row] , I dont want this and cant figure out how to turn it off.

+2  A: 

you can use setSelectionMode of the QAbstractItemView class (which QTreeWidget is inherited from) to set no selection mode to the component. Smth like this (sorry code in c++)

yourtreeView->setSelectionMode(QAbstractItemView::NoSelection);

In this case items would not get selected but you still will see focus rectangle around them. To fix this you can set your widget to not accept focus by calling:

yourtreeView->setFocusPolicy(Qt::NoFocus);

if your treewidget has to accept focus but should not be drawing focus rectangles you can use custom item delegate and remove State_HasFocus state from the item's state before drawing it. Smth like this:

class NoFocusDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};

void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (itemOption.state & QStyle::State_HasFocus)
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
    QStyledItemDelegate::paint(painter, itemOption, index);
}

....

NoFocusDelegate* delegate = new NoFocusDelegate();
yourtreeView->setItemDelegate(delegate);

hope this helps, regards

serge_gubenko
Thanks a lot, i got lost with setSelectionModel(), didnt think i'd find the answer in QAbstractItemView thats fo sure, thanks Serge.
spearfire