开发者

QTreeWidget turn off selection

开发者 https://www.devze.com 2022-12-16 02:15 出处:网络
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 don\'t w

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 don't want this and cant figure out how to turn it off.


you can use setSelectionMode of the QAbstractItemView class (which QTreeWidget is inherited from) to set no selection mode to the component. Something 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 tree widget 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. Something 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);


Thanks for the answer above, I think the Python version is (^ ^):

yourtreeView.setSelectionMode(QAbstractItemView.NoSelection)
yourtreeView.setFocusPolicy(QtCore.Qt.NoFocus)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号